How do you call a function from another function in javascript?

I just want to know how to call a javascript function inside another function. If I have the code below, how do I call the second function inside the first?

function function_one()
{
alert("The function called 'function_one' has been called.")
//Here I would like to call function_two.
}

function function_two()
{
alert("The function called 'function_two' has been called.")
}

SandPiper

2,6414 gold badges27 silver badges45 bronze badges

asked Dec 24, 2010 at 7:26

Web_DesignerWeb_Designer

69.7k89 gold badges203 silver badges261 bronze badges

function function_one() {
    function_two(); // considering the next alert, I figured you wanted to call function_two first
    alert("The function called 'function_one' has been called.");
}

function function_two() {
    alert("The function called 'function_two' has been called.");
}

function_one();

A little bit more context: this works in JavaScript because of a language feature called "variable hoisting" - basically, think of it like variable/function declarations are put at the top of the scope (more info).

answered Dec 24, 2010 at 7:28

ChristianChristian

26.9k15 gold badges108 silver badges153 bronze badges

2

function function_one() {
  function_two(); 
}

function function_two() {
//enter code here
}

Diganta

2451 gold badge7 silver badges21 bronze badges

answered Nov 13, 2012 at 11:25

How do you call a function from another function in javascript?

0

function function_one()
{
    alert("The function called 'function_one' has been called.")
    //Here u would like to call function_two.
    function_two(); 
}

function function_two()
{
    alert("The function called 'function_two' has been called.")
}

Icarus

1,6197 gold badges17 silver badges31 bronze badges

answered Aug 24, 2016 at 13:09

How do you call a function from another function in javascript?

function function_first() {
    function_last(); 
    alert("The function called 'function_first' has been called.");
}

function function_last() {
    alert("The function called 'function_last' has been called.");
}

function_first();

answered Nov 30, 2021 at 12:01


Method Reuse

With the call() method, you can write a method that can be used on different objects.


All Functions are Methods

In JavaScript all functions are object methods.

If a function is not a method of a JavaScript object, it is a function of the global object (see previous chapter).

The example below creates an object with 3 properties, firstName, lastName, fullName.

Example

const person = {
  firstName:"John",
  lastName: "Doe",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}

// This will return "John Doe":
person.fullName();

Try it Yourself »

In the example above, this refers to the person object.

this.firstName means the firstName property of this.

Same as:

this.firstName means the firstName property of person.


What is this?

In JavaScript, the this keyword refers to an object.

Which object depends on how this is being invoked (used or called).

The this keyword refers to different objects depending on how it is used:

In an object method, this refers to the object.
Alone, this refers to the global object.
In a function, this refers to the global object.
In a function, in strict mode, this is undefined.
In an event, this refers to the element that received the event.
Methods like call(), apply(), and bind() can refer this to any object.


The JavaScript call() Method

The call() method is a predefined JavaScript method.

It can be used to invoke (call) a method with an owner object as an argument (parameter).

With call(), an object can use a method belonging to another object.

This example calls the fullName method of person, using it on person1:

Example

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
const person1 = {
  firstName:"John",
  lastName: "Doe"
}
const person2 = {
  firstName:"Mary",
  lastName: "Doe"
}

// This will return "John Doe":
person.fullName.call(person1);

Try it Yourself »

This example calls the fullName method of person, using it on person2:

Example

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
const person1 = {
  firstName:"John",
  lastName: "Doe"
}
const person2 = {
  firstName:"Mary",
  lastName: "Doe"
}

// This will return "Mary Doe"
person.fullName.call(person2);

Try it Yourself »

The call() Method with Arguments

The call() method can accept arguments:

Example

const person = {
  fullName: function(city, country) {
    return this.firstName + " " + this.lastName + "," + city + "," + country;
  }
}

const person1 = {
  firstName:"John",
  lastName: "Doe"
}

person.fullName.call(person1, "Oslo", "Norway");

Try it Yourself »



Can I call a function inside another function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can we use function inside function in JavaScript?

Yes. This method is called currying.

How do you call a function that returns another function?

Approach: First call the first function-1. Define a function-2 inside the function-1. Return the call to the function-2 from the function-1.

What are 3 ways to call a function in JS?

JavaScript functions can be called:.
As a function..
As a method..
As a constructor..
via call and apply..