Functions inside functions
This is a very important concept to understand for fully understanding closures. Take a look at the example code below. It has a method called scopeCheck()
inside which there is another function called innerScopeCheck()
. Outside the function, there is a variable called globalScope
, another variable that gets the return value of the scopeCheck() method and a couple of console.log()
statements.
xxxxxxxxxx
12
const scopeCheck = () => {
let localScope = "LOCAL";
const innerScopeCheck = () => {
let innerLocalScope = "INNER LOCAL";
return globalScope + " { " + localScope + " { " + innerLocalScope + " }} ";
}
return innerScopeCheck;
}
let globalScope = "GLOBAL";
let thisIsAFunction = scopeCheck();
console.log(thisIsAFunction);
console.log(thisIsAFunction());
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment