Hoisting Is Not Moving Code
Hoisting is a useful informal term for the fact that declarations are processed before execution of their surrounding code. It does not mean JavaScript literally moves source lines to the top.
Function declarations can generally be called before their declaration. var declarations are initialized with undefined, while let and const bindings exist but cannot be accessed before initialization because of the Temporal Dead Zone.
Different Declaration Behavior
sayHello();
function sayHello() {
console.log('Hello');
}
console.log(value);
var value = 10;
// console.log(count); // ReferenceError
let count = 10;💡 The function declaration is available during execution. The var binding exists with undefined before its assignment. The let binding exists but cannot be accessed before initialization.
Key Takeaways
- Hoisting is about declaration instantiation, not textual movement.
varcan be observed as undefined before assignment.letandconsthave a Temporal Dead Zone before initialization.
Knowledge Check
What happens when code accesses a `let` variable before its declaration is initialized?