Open In App
Related Articles

How to call a function that return another function in JavaScript ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method.

We’re going to discuss a few techniques. 

Approach:

  • First, call the first function-1.
  • Define a function-2 inside function-1.
  • Return the call to function-2 from function-1.

Example 1: In this example, “from function 2” is returned from the fun2 which is finally returned by fun1

Javascript




function fun1() {
    function fun2() {
        return "From function fun2";
    }
    return fun2();
}
 
function GFG_Fun() {
    console.log(fun1());
}
GFG_Fun()


Output

From function fun2

Example 2: In this example, “Alert from fun2” is returned from the fun2 along with an alert, Returned value is finally returned by fun1

Javascript




function fun1() {
    function fun2() {
        console.log("From function fun2");
        return "Alert from fun2 ";
    }
    return fun2();
}
 
function GFG_Fun() {
    console.log(fun1());
}
GFG_Fun()


Output

From function fun2
Alert from fun2 
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Jun, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials