Beginner JavaScript Interview QuestionsBeginnerConcept
JavaScript · Question 7
What is hoisting in JavaScript?
Direct answer
Hoisting describes how declarations are processed before execution: function declarations are initialized early, var is initialized to undefined, while let, const, and class remain uninitialized until their declaration is evaluated.
Saying “JavaScript physically moves declarations to the top” is a teaching shortcut. A more accurate model is that bindings are created during environment setup before normal statement execution begins.
- Function declarations can generally be called before the declaration appears in source code.
- var exists before its declaration line and initially has the value undefined.
- let, const, and class also have bindings before the declaration line, but accessing them too early throws a ReferenceError because they are in the temporal dead zone.
console.log(count); var count = 1; logs undefined, whereas console.log(count); let count = 1; throws a ReferenceError.