Can you write nested functions in JavaScript?

前端 未结 5 1046
不知归路
不知归路 2020-11-28 20:47

I am wondering if JavaScript supports writing a function within another function, or nested functions (I read it in a blog). Is this really possible?. In fact, I have used t

5条回答
  •  感情败类
    2020-11-28 21:15

    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));

提交回复
热议问题