Scope Chain Interview Questions in JavaScript
Scope chain is a hot interview topic. Here are the most asked questions with clear answers and code.
Scope Chain Interview Questions in JavaScript
The scope chain is a HOT interview topic. Here are the most common questions, with answers and code to practice.
Q1: What is the output?
var x = 1; function foo() { console.log(x); var x = 2; console.log(x); } foo(); console.log(x); `` **Answer**: `undefined`, `2`, `1`. The inner `var x` is hoisted, shadowing the outer. First log sees `undefined`, then `2`. The outer `x` is unchanged. ### Q2: What is the output? ```js function foo() { console.log(x); } function bar() { var x = 10; foo(); } var x = 99; bar(); `` **Answer**: `99`. `foo` was written in the global scope, so its chain is `foo -> global`. Calling it from `bar` does not give it access to `bar`'s `x`. Lexical scope. ### Q3: What is the output? ```js const a = 1; function foo() { const b = 2; function bar() { const c = 3; console.log(a, b, c); } bar(); } foo(); `` **Answer**: `1 2 3`. `bar`'s scope chain is `bar -> foo -> global`. All three variables resolve. ### Q4: What is the output? ```js function makeCounter() { let count = 0; return () => ++count; } const c = makeCounter(); console.log(c(), c(), c()); `` **Answer**: `1 2 3`. The returned arrow keeps a reference to `makeCounter`'s lexical environment. `count` persists and increments. ### Q5: What is the output? ```js var x = 10; function foo() { console.log(x); } (function () { var x = 20; foo(); })(); `` **Answer**: `10`. `foo` was written in the global scope, so it sees the global `x`. The IIFE's `x` is not in `foo`'s chain. ### Q6: What is the output? ```js let x = 1; { let x = 2; console.log(x); } console.log(x); `` **Answer**: `2`, `1`. The block's `let x` shadows the outer. Inside the block, `x` is `2`. Outside, the original `1` is visible. ### Tips for Interviews - Identify where each function was **written** (lexical scope). - Walk the scope chain from the current scope up to global. - Remember: `var` is function-scoped, `let`/`const` are block-scoped. - Remember: hoisting + shadowing can cause the "undefined" bug. - Closures: the chain persists after the outer function returns. ### The Takeaway Scope chain questions test lexical scope, shadowing, hoisting, and closures. Identify where functions are written, walk the chain, account for hoisting, and remember that the chain does not change based on where a function is called.
Identify where each function was written (lexical scope), walk the scope chain from the current scope up to global, account for hoisting and shadowing, and remember that the chain does not change based on where the function is called.
Because JavaScript is lexically scoped. The scope chain is determined by where the function was written, not where it is called. The caller's variables are not in the function's scope chain.
var shadowing plus hoisting. An inner var with the same name as an outer variable is hoisted with undefined, so a read before the assignment line sees undefined instead of the outer value.
Closures keep the scope chain alive after the outer function returns. The inner function can still access the outer variables. This is why makeCounter returns a function that keeps incrementing.
var is function-scoped and leaks out of blocks; let and const are block-scoped. var is hoisted with undefined; let and const are hoisted but in the TDZ. This difference changes the output of many interview questions.
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.

