How do you call a function within a function in javascript?

I am calling a function on button click like this:

​

function outer() { 
    alert("hi");       
}

It works fine and I get an alert:

Now when I do like this:

function outer() { 
    function inner() {
        alert("hi");
    }
}

Why don't I get an alert?

Though inner function has a scope available in outer function.

How do you call a function within a function in javascript?

asked Nov 4, 2012 at 12:22

1

You could make it into a module and expose your inner function by returning it in an Object.

function outer() { 
    function inner() {
        console.log("hi");
    }
    return {
        inner: inner
    };
}
var foo = outer();
foo.inner();

answered Dec 18, 2015 at 21:29

How do you call a function within a function in javascript?

Matt HolmesMatt Holmes

8298 silver badges6 bronze badges

3

The scoping is correct as you've noted. However, you are not calling the inner function anywhere.

You can do either:

function outer() { 

    // when you define it this way, the inner function will be accessible only from 
    // inside the outer function

    function inner() {
        alert("hi");
    }
    inner(); // call it
}

Or

function outer() { 
    this.inner = function() {
        alert("hi");
    }
}

answered Nov 4, 2012 at 12:23

How do you call a function within a function in javascript?

techfoobartechfoobar

64.2k14 gold badges113 silver badges130 bronze badges

3

You are not calling the function inner, just defining it.

function outer() { 
    function inner() {
        alert("hi");
    }

    inner(); //Call the inner function

}

answered Nov 4, 2012 at 12:23

EsailijaEsailija

136k23 gold badges266 silver badges319 bronze badges

You can also try this.Here you are returning the function "inside" and invoking with the second set of parenthesis.

function outer() {
  return (function inside(){
    console.log("Inside inside function");
  });
}
outer()();

Or

function outer2() {
    let inside = function inside(){
      console.log("Inside inside");
    };
    return inside;
  }
outer2()();

answered Nov 17, 2017 at 11:15

TharzeezTharzeez

1,18514 silver badges15 bronze badges

Again, not a direct answer to the question, but was led here by a web search. Ended up exposing the inner function without using return, etc. by simply assigning it to a global variable.

var fname;

function outer() {
    function inner() {
        console.log("hi");
    }
    fname = inner;
}

Now just

fname();

answered Feb 23, 2021 at 8:20

rshawrshaw

913 bronze badges

If you want to call the "inner" function with the "outer" function, you can do this:

function outer() { 
     function inner() {
          alert("hi");
     }
     return { inner };
}

And on "onclick" event you call the function like this:

answered Apr 17, 2020 at 19:27

How do you call a function within a function in javascript?

you can also just use return:

   function outer() { 
    function inner() {
        alert("hi");
    }
return inner();

}
outer();

answered Oct 8, 2020 at 5:58

How do you call a function within a function in javascript?

function outer() { 

    function inner() {
        alert("hi");
    }
    inner();
}

you should try this

answered Oct 8, 2021 at 5:51

How do you call a function within a function in javascript?

fazalfazal

236 bronze badges

1

In JavaScript

If using ES6
static functions can be used in a class

If using ES5
After several days of usage, below is what I came up with,
it is minimal & also has a lot of conveniences:

function MathFunctions() {
    let thefo = {}; // the functions object

    thefo.sum = sum = (a, b) => {
        return a + b;
    };
    thefo.avg = avg = (a, b) => {            // name is repeated 2 times - minor inconvenience
        return sum(a, b) / 2;                // calls sum, another function without using 'this'
    };

    return thefo;                            // no need to go down & export here always for each new function - major convenience
}

// Usage
console.log(MathFunctions().sum(1, 2));
console.log(MathFunctions().avg(1, 2));
// OR
const mf = MathFunctions();
console.log(mf.sum(1, 2));
console.log(mf.avg(1, 2));

answered Jan 13 at 2:57

How do you call a function within a function in javascript?

Try This Solution

const greet = (greeting) => {
    return function (name) {
        console.log(`${greeting} ${name}`);
    };
};

greet('Hi !')('JAHID');

How do you call a function within a function in javascript?

function parent() {
    this.child1 = function child1(string){
        console.log(string);
    }
    this.child2 = function child2(string){
        console.log(string);
    }
}

new parent().child1("hello world child1");
new parent().child2("hello world child2");

Output:

"hello world child1"

"hello world child2"

answered Apr 5 at 16:15

How do you call a function within a function in javascript?

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

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.

What do you call a function within a function?

A function defined inside another function is known as an inner function or a nested function.

How do you call an inner function?

Approach:.
Write one function inside another function..
Make a call to the inner function in the return statement of the outer function..
Call it fun(a)(b) where a is parameter to outer and b is to the inner function..
Finally return the combined output from the nested function..

Is it possible to nest functions in JavaScript?

JavaScript (JS) has allowed nested functions without any errors. JS allows anonymous functions also inside an outer function. Child function can access all values global, local and parent.