Nov 29, 2016

Closures In Javascript

Actual understanding : Closure is a function that stays alive or connected to its parent Scope or parent function even after Parent scope or parent functions execution is over.


Problem : Suppose you are told to write a function that increments counter value by 1, What will you do ?


Obviously We would Code
var counter = 0;

function add() {
    return counter += 1;
}
add(); //Outputs 1
add(); //Outputs 2
add(); //Outputs 3