Home > Net >  Does Closure exist in a Nested function without variables
Does Closure exist in a Nested function without variables

Time:12-05

Can we say a closure exist in this code

function foo(){
        return function foo2(){
                   console.log("Inner Function");
          }
}

let x = foo();
x();

CodePudding user response:

No the inner function is not a closure here. It is not accessing any variable which is outside the scope.

Here is the snapshot from chrome console.

Inner function

But when the inner function access a variable defined outside its scope the whole environment that is function with references to its surrounding becomes a closure. You can see scope closure in the console

Closure

  • Related