JavaScript · Hoisting · Fresh / Intern · Concept
What is hoisting in JavaScript?
Short Interview Answer
Hoisting is the behavior where declarations are processed during the creation phase of an execution context before code runs, so bindings exist before their line in source.
Detailed Explanation
During context creation, the engine registers declarations: function declarations are initialized fully; `var` is registered and set to `undefined`; `let`/`const`/class are registered but left in the TDZ. People summarize this as 'declarations are moved to the top,' but nothing is rewritten — it is environment setup. Understanding hoisting prevents bugs with use-before-define and explains differences between declaration kinds.
Example
sayHi();
function sayHi() {
console.log('hi');
}Interview Tip
Prefer the execution-context explanation over the 'code is moved' metaphor.
Common Mistake
Saying assignments are hoisted — only the declaration binding is; `var x = 5` still assigns at that line.