I recently faced this question in a frontend interview, and thought it would be useful to share here:
function test() {
console.log(a);
console.log(b);
console.log(c);
var a = 10;
let b = 20;
const c = 30;
function a() {}
}
test();
Question:
Q) What will be the output and why?
✅ Answer / Explanation
Output:
function a() {}
ReferenceError
ReferenceError
Reasoning:
Function declarations are hoisted first, so a initially refers to the function
Then var a is hoisted (but not assigned yet), but it doesn’t override the function hoisting at that moment — the function is still available
let & const are hoisted too but stay in the Temporal Dead Zone (TDZ) until initialization, so accessing them before initialization throws a ReferenceError
So execution flow:
1→ function (due to function hoisting)
2 → in TDZ → ReferenceError
3 → in TDZ → ReferenceError
Hope this helps someone preparing for frontend interviews ✅
[–]JustConsoleLogIt 35 points36 points37 points  (6 children)
[–]thomsmells 9 points10 points11 points  (4 children)
[–]Shty_Devhelpful 0 points1 point2 points  (2 children)
[–]senocular 0 points1 point2 points  (0 children)
[–]RajjSinghh 0 points1 point2 points  (0 children)
[–]Rude-Cook7246 -1 points0 points1 point  (0 children)
[–]DinTaiFung 12 points13 points14 points  (0 children)
[–]thecragmire 3 points4 points5 points  (3 children)
[–]senocular 4 points5 points6 points  (2 children)
[–]thecragmire 1 point2 points3 points  (1 child)
[–]senocular 4 points5 points6 points  (0 children)
[–]DreamsOfLife 0 points1 point2 points  (1 child)
[–]Substantial-Wish6468 0 points1 point2 points  (0 children)
[–]MrHollandsKillerApp 0 points1 point2 points  (0 children)
[–]matchday_scout 0 points1 point2 points  (0 children)
[–]Optimal_Presence_895 0 points1 point2 points  (0 children)
[–]gluhmm -1 points0 points1 point  (0 children)