Vocabulary Around Variables and Scope
Vocabulary Around Variables and Scope
JavaScript Cheatsheet
Page 3
function first(){
Global scope
Variable Initialization The outer most scope is called the Global var a = "fresh";
a = 12; The initial scope.
assignment of value
function second(){
to a variable.
Functional scope console.log(a);
Any variables inside a function is in scope }
a = "me"; Variable Assignment of the function.
}
Assigning value to a
variable.
Lexical Environment (Lexical scope) Scope chain
The physical location (scope) where a The nested hierarchy of scope is
console.log(a); Hoisting variable or function is declared is its lexical called the scope chain. The JS
Variables are environment (lexical scope). engine looks for variables in the
var a = "me";
declared at the top scope chain upwards (it its
of the function Rule: ancestors, until found)
automatically, and (1) Variables in the outer scope can be
initialized at the time accessed in a nested scope; But variables
they are run. inside a nested scope CANNOT be accessed
by the outer scope. (a.k.a private variables.)