By definition, a JavaScript closure ensures that the variable is a combination of a function along with references to its surrounding state. To understand this a little more clearly, we will make use of the Inspect command in Google Chrome. The below code snippet is a slight modification of the previous one.
xxxxxxxxxx
14
const scopeCheck = () => {
let b = 20;
const innerScopeCheck = () => {
let a = 10;
let ret = a + b;
b++;
return ret;
}
return innerScopeCheck;
}
let fun1 = scopeCheck();
let fun2 = scopeCheck();
console.dir(fun1);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment