It uses an improved algorithm to declare variables and additionally is block scoped.īecause the declaration and initialization phases are decoupled, hoisting is not valid for a let variable (including for const and class). The freedom to declare variables using var is error prone.īased on this lesson, ES2015 introduces let. In a sci-fi style, the collapsed hoisting in let lifecycle creates the temporal dead zone. The gap between the two phases creates the temporal dead zone, where the variable cannot be accessed. Decoupling vanishes the hoisting term for let. Let lifecycle however decouples declaration and initialization phases. 5.1 Why hoisting is not valid in let lifecycleĪs mentioned above, hoisting is variable's coupled declaration and initialization at the top of the scope. The assignment statement number = 5 of course makes the assignment phase.Ĭonst and class types have the same lifecycle as let, other than the assignment can happen only once. Now the variable can be accessed, but its value is undefined. Later the statement let number makes the initialization. The function function getPi() block scope, number instantly passes the declaration phase.īecause number has unitialized state and is in a temporal dead zone, an attempt to access the variable throws ReferenceError: number is not defined. The variable num is accessed before declaration var num, so it is evaluated to undefined.
0 Comments
Leave a Reply. |