How to call a Function inside another Function in JS

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Call a Function inside another Function in JavaScript

To call a function inside another function, define the inner function inside the outer function and invoke it.

When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside the outer function.

index.js
function outerFunc(a, b) { function innerFunc(a, b) { return a + b; } const result = innerFunc(a, b); return result; } console.log(outerFunc(10, 10)); // ๐Ÿ‘‰๏ธ 20 console.log(outerFunc(10, 20)); // ๐Ÿ‘‰๏ธ 30

call function inside another function

The code for this article is available on GitHub

The following example shows how we can call the inner function before it is declared.

This is because of how hoisting works in JavaScript.

index.js
function outerFunc() { const num1 = 5; const num2 = 10; // ๐Ÿ‘‡๏ธ call inner function before it's declared const result = innerFunc(); function innerFunc() { return num1 + num2; } return result; } console.log(outerFunc()); // ๐Ÿ‘‰๏ธ 15

how hoisting works in javascript

The code for this article is available on GitHub

This only works for functions declared using the function keyword (not for arrow functions).

You can imagine that the declaration of the function gets hoisted to the top of the scope, so it can be called from anywhere in the scope.

# Returning the inner function from the outer function

An alternative approach is to return the inner function from the outer one.

index.js
function outerFunc() { function innerFunc(a, b) { return a + b; } return innerFunc; } const innerFunc = outerFunc(); console.log(innerFunc(2, 3)); // ๐Ÿ‘‰๏ธ 5 console.log(innerFunc(3, 3)); // ๐Ÿ‘‰๏ธ 6

returning inner function from outer function

The code for this article is available on GitHub

Notice that we didn't use parentheses () to invoke the inner function inside the outer one.

We returned the function without invoking it. In other words, we returned a reference to the inner function, not the result of calling it.

This allows us to invoke the inner function as many times as necessary, passing it different arguments every time.

# Inner function remembers variables declared in the outer function

What's most useful in this scenario is that the inner function remembers the variables declared in the outer function between invocations.

index.js
function outerFunc() { const z = 100; function innerFunc(a, b) { return a + b + z; } return innerFunc; } const innerFunc = outerFunc(); console.log(innerFunc(2, 3)); // ๐Ÿ‘‰๏ธ 105 console.log(innerFunc(3, 3)); // ๐Ÿ‘‰๏ธ 106

inner function remembers variables declared in outer function

The code for this article is available on GitHub

Notice that the inner function remembers the value of the z variable between invocations.

This concept is called a closure in JavaScript.

The inner function gets bundled with references to its surrounding state.

This means that the inner function has access to the variables declared inside of the outer function's scope at any time.

This is useful in many different scenarios. For example, you could pass a parameter to the outer function that it will remember for any of the inner function calls.

index.js
function outerFunc(a) { function innerFunc(b, c) { return a + b + c; } return innerFunc; } const innerFunc = outerFunc(10); console.log(innerFunc(1, 1)); // 12 console.log(innerFunc(1, 2)); // 13

We passed 10 as a parameter to the outer function and stored the result in a variable.

The innerFunc variable stores a reference to the innerFunc function where the a variable points to a value of 10.

Now we can omit 10 from the parameters when calling the inner function.

# Returning an object from the outer function

You can also return an object from the outer function.

index.js
function outerFunc() { function innerFunc(a, b) { return a + b; } return {innerFunc}; } const outer = outerFunc(); console.log(outer.innerFunc(10, 10)); // 20 console.log(outer.innerFunc(20, 15)); // 35
The code for this article is available on GitHub

The object contains a single property - the inner function.

You can call the inner function by accessing the property on the object.

I've written a detailed article on how to call a function in an object.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev