Can you write nested functions in JavaScript?

Is this really possible.

Yes.

function a(x) {    // <-- function
  function b(y) { // <-- inner function
    return x + y; // <-- use variables from outer scope
  }
  return b;       // <-- you can even return a function.
}
console.log(a(3)(4));

Leave a Comment