Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does Closure exist in a Nested function without variables

Can we say a closure exist in this code

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

let x = foo();
x();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading