What is closures in Javascript?
In Javascript a closure is considered an inner function that has access to the outer function’s variables. The function that is defined in the closure remembers the environment in which the function was created.
The closures has three levels of access of the scope chain.
1. It has access to it's local variables
2. It has access to it's outer function’s variables
3. It has access to the global variables
Let say we want to multiply two numbers using Javascript closures.
function doMultiplication(num1) { return function(num2) { return num1 * num2; }; } var multiply = doMultiplication(10); console.log(multiply(3)); console.log(multiply(5));
Output
30 50
Comments
Post a Comment