Skip to main content

Posts

Showing posts from September, 2017

Javascript Closure | Simply Explained

In Javascript world the things are always confused. The most confused and made complicated feature is closure. But the truth is Javascript Closure is really Simple Let me start with the simple below javascript function function simpleClosure(passed) {    var inner = 20;    return inner + passed; } simpleClosure(10); The above function is the simple example for closure.  When simpleClosure function executed, the passed(argument)  value will be added in simpleClosure lexical scope. Below code will give you better understanding. var addTen = simpleClosure(10); console.log(addTen); // Outputs 10 I am going to do the simple change in simpleClosure function. I moved passed argument variable to outer scope. var passed = 10; function simpleClosure() { var inner = 20; return inner + passed; } console.dir(simpleClosure); Please  go through the below output in the console, where you can see passed value 10 is under closure. Lets see a bit complex closur