TIL Lexical Scoping
POSTED ON:
TAGS: javascript closure
Lexical scoping is a part of Closures.
Lexical scoping describes how a parser resolves variable names when functions are nested. Lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Nested functions have access to variables declared in their outer scope.
function initialize() {
var name = 'Mozilla'; // name is a local variable created by initialize
function displayName() { // displayName() is the inner function, a closure
alert(name); // the variable declared in the parent function
}
displayName(); // will fire the displayName function
}
initialize();
This is the example in the MDN.
The variable name
will stay stay and remain available for us.
Related TILs
Tagged: javascript