Basic of Closure in JavaScript.

Basic of Closure in JavaScript.

A Closures is created when an inner function tries to access the scope chain of its outer function.

Closures contains their own scope chain, the scope chain of their parents, and the global scope.

A closure can also access the variables of its outer function even after the function has returned. This allows the returned function to maintain access to all the resources of the outer function.

When you returned an inner function from function, that returned function will not be called when you try to call the outer function.

for example :

function greet(){
    name = "Akshar";
    return function(){
        console.log('Hello' +" "+ name);
    }
}
greet(); // nothing happens

//return function from greet() gets save in greeetLetter
greetLetter = greet();
greetLetter(); //logs 'Hello Akshar"