- let is block scope. You can reassign values, but cannot redefine the variable.
- const is block scope. You cannot reassign or redefine with const.
- var is function scope. You can reassign values and redefine the variable. It is not as widely used as before because function scope can cause confusion.
window is an object that covers globally. Therefore, function declared in global scope and variables that are defined with var are connect to window.
There are two ways to determine a function’s scope at a higher level. The first is by where the function was called and the second is where the function was declared.
- Hoisting by definition means to lift up, and can be interpreted as lifting up the declared variable or function to the highest level of code.
- According to MDN, the declaration of variable or funciton is saved in memory in compilation stage, but is located where the code is written at.
The first console log brings only the var height part due to hoisiting and will not cause error even when var height = 176 occurs after. However, since the assignment is not hoisted, it prints undefined. The second console log correctly prints 176.
Function declaration overrides variable declaration after hoisiting,
When a value is assigned to the variable, the variable overrides the function declaration.
Variable Declaration + Assignment > Function Declaration > Variable Declaration;
Scope and Hoisting Review finished!