Common Hoisting Interview Questions in JavaScript
Hoisting is a top JS interview topic. Here are the most asked questions with clear answers and code examples.
Common Hoisting Interview Questions in JavaScript
Hoisting is asked in almost every JavaScript interview. Here are the most common questions, with answers and code you can practice.
Q1: What is the output?
console.log(a); var a = 5;
Answer: undefined. a is hoisted with undefined. The assignment runs at the line.
Q2: What is the output?
foo(); function foo() { console.log("hi"); }
Answer: "hi". Function declarations are hoisted with their full body.
Q3: What is the output?
bar(); var bar = function () { console.log("hi"); };
Answer: TypeError: bar is not a function. bar is hoisted as undefined.
Q4: What is the output?
console.log(x); let x = 5;
Answer: ReferenceError: Cannot access 'x' before initialization. let is in the TDZ.
Q5: What is the output?
var a = 1; function foo() { console.log(a); var a = 2; console.log(a); } foo(); console.log(a);
Answer: undefined, 2, 1. Inside foo, the local var a is hoisted (shadowing the outer one), so the first log is undefined. Then a = 2 runs. The outer a is untouched.
Q6: What is the output?
function foo() { console.log("1"); } foo(); function foo() { console.log("2"); } foo();
Answer: "2", "2". The second function declaration overrides the first during hoisting.
Q7: What is the output?
console.log(typeof x); var x;
Answer: "undefined". typeof on a hoisted var returns "undefined" without throwing.
Q8: What is the output?
console.log(typeof y); let y;
Answer: ReferenceError. Even typeof cannot escape the TDZ.
Tips for Interviews
- Walk through the memory phase first (what gets hoisted to what value).
- Then walk through the code execution phase line-by-line.
- Mention TDZ whenever
let/constis involved. - Mention that function expressions follow variable hoisting rules, not function declaration rules.
The Takeaway
Hoisting interview questions test the memory phase, the TDZ, shadowing, and the difference between function declarations and expressions. Walk through both phases (memory then execution) for each question.
undefined. The var declaration is hoisted and initialized to undefined. The assignment a = 5 runs only when the line is reached, so the earlier log sees undefined.
TypeError: x is not a function. The var variable is hoisted with undefined, so calling it as a function fails. Only function declarations are hoisted with their body.
No. typeof on a let or const variable in the TDZ throws ReferenceError. typeof on a var that has not been assigned returns the string 'undefined' without throwing.
The last one wins. Both are hoisted with their full bodies, and the later declaration overwrites the earlier one in memory. So calls after both declarations always invoke the last one.
Walk through the memory phase first: what is hoisted and to what value (undefined for var, full body for function declarations, TDZ for let/const). Then walk the execution phase line-by-line to determine each output.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

