Variable Hoisting — JS Technical Interview Questions & Answers
How var/let/const declarations are hoisted differently.
Variable Hoisting
- Variable Hoisting (js) is a JS topic. In plain words you use it for let total = 0 in a cart script. Don’t start with a slogan — start with that picture.
- Smallest example: let for change, const for fixed. Type it, run it, and say what you see. If you can do that from memory, you know Variable Hoisting (js).
- From the example next to this theory: How var/let/const declarations are hoisted differently.
- Trap: var leak / const reassign. Fix that before you talk about advanced JS.
- Viva: what is Variable Hoisting (js)? Then show let for change, const for fixed. Then name the trap.
Technical Interview Tip: What is Variable Hoisting (js)? Show this: let for change, const for fixed. Trap: var leak / const reassign.
Code Examples for Variable Hoisting
Variable Hoisting — working example
function demo() {
if (true) {
var a = 1;
let b = 2;
const c = 3;
console.log(b, c);
}
console.log(a); // 1 — var is function-scoped
// console.log(b); // ReferenceError — let is block-scoped
}
demo();
console.log(typeof undeclaredVar); // undefined (never declared with let/const)Variable Hoisting — var is function-scoped and hoisted; let/const are block-scoped. Prefer const, then let.
How to Explain Variable Hoisting to an Interviewer
What is Variable Hoisting (js)?
Used for let total = 0 a cart script Not just the heading
Give one small example of Variable Hoisting (js).
let for change, const for fixed How var/let/const declarations are hoisted differently
What mistake do beginners make with Variable Hoisting (js)?
var leak / const reassign Show the fix
Where do you use Variable Hoisting (js)?
a cart script let total = 0