Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptHow to call a function that return another function in JavaScript ?

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

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 

RELATED ARTICLES

Most Popular

Recent Comments