-1

What is the meaning of returning a function from another function in javascript. Is this is same as if I call a function which is in another function

function fun(c){
    let a=3;
     return function(){
        return a+c;
    }
}
let add=fun(2);
console.log(add);

. And my second question is why this add variable is a function.

Ishan Ahmed
  • 105
  • 11

2 Answers2

1

Currently, the object which you are returning is the function. In this context, variable add contains a reference to the function which you have to invoke.

1

This is called currying. It allows you to apply arguments to functions one by one. Example:

const add = a => b => a + b // Function that takes `a` and returns a function that adds `a` to `b`
const addThree = add(3) // bind `a` to 3. This is a function that takes `b` and adds 3 to it
console.log(addThree(1)) // now `b` is 1, it's gonna be added to `a` and return the final value

There's a lot of literature about currying and partial application. Google is your friend

bel3atar
  • 913
  • 4
  • 6