JavaScript Hoisting and Scope Interview Questions
Last updated 2026-11-19
Hoisting and scope questions are a rite of passage in JavaScript interviews. They look like trivia, but they test whether you understand how the engine creates scopes and binds names before running a single line.
The answers below explain what is hoisted, what is initialised, and when a name becomes accessible, followed by the output puzzles that interviewers use most: var inside functions, function declarations competing with variables, and the temporal dead zone.
| Level | Fresher to 3 years |
|---|---|
| Focus | Declarations, scope chain, temporal dead zone |
| Runtime | Browser and Node.js |
The two phases
Before running a scope, the engine scans it and creates bindings for every declaration. That is all hoisting is: var gets a binding initialised to undefined, function declarations get a binding initialised to the whole function, and let, const and class get a binding that stays uninitialised until the declaration line executes.
- var: function-scoped, initialised to undefined
- function declaration: hoisted with its body
- let, const, class: block-scoped, temporal dead zone until declared
- Function expressions and arrow functions follow the rules of the variable that holds them
Next topics
Scope is the foundation of closures, and the modern declarations are summarised in ES6 features. See how to crack a JavaScript interview for the full plan.
JavaScript Hoisting and Scope Interview Questions: questions with worked answers
1 What is hoisting?
easyHoisting is the behaviour where declarations are processed before the code in their scope runs, so names are known throughout the scope even before the line that declares them. Code is not physically moved. var declarations are hoisted and initialised to undefined, function declarations are hoisted with their value, and let, const and class declarations are hoisted but uninitialised, so reading them early throws a ReferenceError.
2 Predict the output: var hoisting.
easyThe declaration var x is hoisted to the top of its scope and initialised to undefined, but the assignment stays where it is. The first log therefore sees undefined, not a ReferenceError.
javascriptconsole.log(x); // undefined var x = 5; console.log(x); // 5 // the engine treats it as: // var x; console.log(x); x = 5; console.log(x);3 What is the temporal dead zone?
mediumIt is the span from the start of a block to the line where a let, const or class binding is initialised. The binding exists, so an outer variable of the same name is shadowed, but any access throws a ReferenceError. The name comes from the fact that the zone is defined by time of execution, not by position in the source.
javascriptlet v = "outer"; { try { console.log(v); } catch (e) { console.log(e.message); } // Cannot access 'v' before initialization let v = "inner"; console.log(v); // inner }4 Do function declarations and function expressions hoist the same way?
mediumA function declaration is hoisted with its body, so it can be called before it appears. A function expression assigned to var is hoisted only as a variable initialised to undefined, so calling it early throws a TypeError, not a ReferenceError. With let or const the early call hits the temporal dead zone.
javascriptconsole.log(declared()); // ok function declared() { return "ok"; } try { expr(); } catch (e) { console.log(e.name, e.message); } // TypeError expr is not a function var expr = function () { return "no"; };5 Predict the output: function and var with the same name.
hardFunction declarations are initialised first, and a var declaration with the same name does not overwrite them because it has no initial value. So typeof f is 'function' at the top. When execution reaches the assignment, the variable becomes 1 and the function is gone.
javascriptconsole.log(typeof f); // function var f = 1; function f() {} console.log(typeof f); // number6 Predict the output: shadowing with a hoisted var inside a function.
hardThe var a inside the function is hoisted to the top of that function and initialised to undefined, so it shadows the outer a for the whole body. The first log reads the local undefined, not the outer 1.
javascriptvar a = 1; function f() { console.log(a); // undefined var a = 2; console.log(a); // 2 } f(); console.log(a); // 17 What is the difference between function scope and block scope?
mediumvar is scoped to the nearest function, ignoring blocks such as if and for, so it is visible after the block ends. let and const are scoped to the nearest block, including loops and braces. Function declarations inside blocks are block-scoped in strict mode, but have legacy behaviour in sloppy mode.
javascriptif (true) { var a = 1; let b = 2; } console.log(a); // 1 console.log(typeof b); // undefined (b is not in scope)8 What is the scope chain and lexical scope?
mediumLexical scope means a function's scope is fixed by where it is written, not where it is called. When a name is not found in the current scope, the engine looks in the enclosing scope, then outward to the global scope. That ordered list of scopes is the scope chain. If the name is not found anywhere, reading it throws a ReferenceError.
javascriptconst x = "global"; function outer() { const x = "outer"; function inner() { return x; } return inner; } function caller() { const x = "caller"; return outer()(); } console.log(caller()); // outer, not caller9 What happens when you assign to an undeclared variable?
mediumIn sloppy mode, assigning to a name that was never declared creates a property on the global object, an implicit global, which is a common source of bugs. In strict mode, including ES modules and classes, the same assignment throws a ReferenceError. Reading an undeclared name always throws, except with typeof.
javascriptfunction leak() { total = 10; } leak(); console.log(total); // 10 (sloppy mode only) console.log(typeof missing); // undefined, no error10 Are let and const hoisted?
mediumYes, in the sense that the binding is created when the scope starts, which is why an inner let shadows an outer variable for the entire block. They are not initialised, so access before the declaration throws. Some people say they are not hoisted because they cannot be used early, but the shadowing behaviour proves the binding exists.
11 Predict the output: a closure in a loop declared with var and with let.
hardA var loop variable is one binding for the whole function, so closures see its final value. A let loop variable is created fresh for each iteration, and each closure captures its own copy of the value at that iteration.
javascriptconst fs = []; for (var i = 0; i < 3; i++) fs.push(() => i); console.log(fs.map((f) => f())); // [ 3, 3, 3 ] const gs = []; for (let j = 0; j < 3; j++) gs.push(() => j); console.log(gs.map((g) => g())); // [ 0, 1, 2 ]12 Do classes hoist?
mediumClass declarations are hoisted like let: the binding exists but is in the temporal dead zone until the declaration runs, so creating an instance before the class statement throws a ReferenceError. Class expressions follow the rules of the variable they are assigned to.
javascripttry { new A(); } catch (e) { console.log(e.name); } // ReferenceError class A {} console.log(new A() instanceof A); // true13 Predict the output: default parameter scope and shadowing.
hardDefault parameter expressions are evaluated in their own scope, in order, and can use earlier parameters but not later ones, which would be in their temporal dead zone. A variable with the same name declared in the body with var creates a separate binding.
javascriptfunction f(a, b = a + 1) { return [a, b]; } console.log(f(1)); // [ 1, 2 ] try { (function (x = y, y = 1) {})(); } catch (e) { console.log(e.name); } // ReferenceError14 What does the IIFE pattern solve, and what replaced it?
mediumAn immediately invoked function expression creates a private function scope so variables do not leak into the global scope, which mattered when var was the only declaration. With let, const and ES modules, a simple block or a module file now provides the same isolation.
javascript(function () { var hidden = "inside"; })(); console.log(typeof hidden); // undefined { const alsoHidden = 1; } console.log(typeof alsoHidden); // undefined15 How do var and let differ at the global level in a browser script?
mediumA top-level var in a classic script becomes a property of window, whereas a top-level let or const lives in the script's global lexical environment but is not a property of window. In ES modules and Node.js modules, top-level declarations are scoped to the file and do not become globals at all. Avoid relying on window properties for state.
Frequently asked questions
Is hoisting moving code to the top?
No. It is the engine creating bindings for declarations before running the scope. Source code is not rewritten.
Why does let not return undefined if used early?
Because it is in the temporal dead zone: the binding exists but is uninitialised, and accessing it throws a ReferenceError by design, to catch mistakes.
What is the difference between scope and context?
Scope is about which variables are visible at a point in code. Context, or this, is about the object a function is called on.
Should I use var at all?
Rarely. Use const and let in new code. Know var behaviour for reading older code and for interview questions.
Related guides
More in Programming Language Interview Questions by Language
- JavaScript Interview Questions and Answers
- JavaScript Promises and Async/Await Interview Questions
- JavaScript Prototypes and Inheritance Interview Questions
- JavaScript this Keyword Interview Questions
- JVM Internals Interview Questions
- MySQL Interview Questions and Answers
- Nth Highest Salary in SQL: Interview Questions
- PL/SQL Interview Questions and Answers
- PostgreSQL Interview Questions and Answers
- Python Coding Interview Questions with Solutions
- Python Decorators Interview Questions
- Python Exception Handling Interview Questions