JavaScript Scopes Interview Questions and Answers
Understand lexical scope, declaration visibility, hoisting, and the temporal dead zone.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What is global scope?
- What does "this" refer to in the global scope?
- Which declarations are block-scoped?
- What does lexical scope mean?
- What are the different types of scopes in JavaScript?
- What is the scope chain in JavaScript?
- Explain the purpose and usage of the let keyword in JavaScript. How does let differ from var in terms of scope and hoisting?
Theoretical JavaScript Interview Questions
Before ES6, we only had var, and it caused all sorts of scoping headaches. let was introduced to fix that by giving you block-level scope - a variable declared with let inside an if-block or for-loop stays inside that block.
let also behaves differently with hoisting. While var gets hoisted and initialized as undefined (so you can access it before declaration), let gets hoisted but stays in a "temporal dead zone" until execution reaches the declaration line. Access it too early and you get a ReferenceError instead of a silent undefined - which is actually better because it catches bugs early.
var is the original way to declare variables in JavaScript, and understanding its quirks helps you debug legacy code and ace interview questions.
var has function scope, not block scope. Declare a var inside an if-block and it leaks out into the surrounding function. It also hoists to the top of its scope and initializes as undefined, which means you can use a var before the line where you declared it without getting an error - you'll just get undefined. This silent behavior is a classic bug factory. The classic interview example: a for-loop with var and setTimeout where every iteration prints the same final value. Use let or const in modern code to avoid these traps.
Hoisting means JavaScript moves declarations to the top of their scope before running. Function declarations are fully hoisted - you can call them before they appear in the file. var declarations are hoisted but set to undefined until the assignment runs, which causes subtle bugs. let and const are hoisted too, but in the Temporal Dead Zone until declared, so accessing them early throws a ReferenceError rather than silently returning undefined.
console.log(sayHello()); // "Hello!" - function is fully hoisted
function sayHello() { return "Hello!"; }
console.log(x); // undefined - var hoisted, value is not
var x = 10;
// console.log(y); // ReferenceError - let is in TDZ
let y = 20;
The Temporal Dead Zone (TDZ) is the window between entering a block scope and when a let or const variable is actually declared. During this time, the variable exists in memory but accessing it throws a ReferenceError. This is intentional - it prevents the confusing "undefined before declaration" behavior you get with var. The practical lesson: always declare let/const at the top of the block they're used in.
// TDZ starts here for 'name'
console.log(name); // ReferenceError: Cannot access 'name' before initialization
let name = "Alice"; // TDZ ends here
Scoping is the set of rules that determines where a variable is accessible. JavaScript has lexical scoping, meaning scope is determined by where code is written, not where it's called from. Inner scopes can access outer scope variables, but not vice versa - this is the basis of closures and is one of the most important concepts to internalize.
There are three scopes: global (accessible everywhere), function (accessible only within the function it's declared in), and block (accessible only within the {} block, and only for let/const). The addition of block scope in ES6 was a big deal - it lets you limit a variable's lifetime to exactly the loop or if-branch that needs it, rather than the entire function.
A JS engine takes your JavaScript source code and runs it - parsing, compiling to bytecode, and optimizing hot paths with JIT compilation. V8 (Chrome/Node.js) is the most relevant one for most devs today, SpiderMonkey powers Firefox, and JavaScriptCore (also called Nitro) is in Safari. The engine is what makes Node.js possible - V8 was extracted from Chrome and embedded into a server runtime.
"Heap" is a common implementation term for dynamically managed storage used by JavaScript engines. Objects are garbage-collected when they are no longer reachable, but ECMAScript does not require a specific stack or heap layout, and engines may optimize values differently. A useful interview distinction is reachability: a closure, global, or event listener can keep an object reachable and prevent collection.
When JS can't find a variable in the current scope, it walks up the scope chain - checking each enclosing scope in order until it finds it or hits the global scope (and throws a ReferenceError if it's not there). The scope chain is set at the point a function is defined, not where it's called - that's lexical scoping. Closures are just functions that maintain a reference to their outer scope chain even after that outer function has returned.
At the top level of a classic browser script, this is window. In an ES module, top-level this is undefined. Node.js also has module-specific top-level behavior, so avoid describing one universal global value. For a plain regular-function call, strict mode makes this undefined; sloppy mode may default it to the global object.
// Classic browser script:
console.log(this === window); // true
// ES module:
console.log(this); // undefined
`let` and `const` are scoped to the nearest enclosing block, such as a function body, loop body, or `if` block. `var` is function-scoped instead.
Global scope is the outermost scope available to code in a script or environment. The exact relationship between top-level declarations and global object properties depends on whether code is a classic script, module, or another execution environment.
Lexical scope means name visibility is determined by where code is written in the program. A nested function can access bindings in its enclosing scopes.
JavaScript looks for the binding in the current lexical environment and then searches enclosing environments outward. If no binding is found, evaluating the identifier produces a `ReferenceError`.
A loop declared with `let` creates a per-iteration binding captured separately by each closure. A `var` loop generally has one function-scoped binding shared by all closures, so they observe its final value.
In a non-strict classic script, assigning to an undeclared identifier can create a property on the global object. Strict mode and JavaScript modules instead throw a `ReferenceError`, so declare variables explicitly.
A binding in an inner scope shadows an outer binding with the same name while lookup occurs in that inner scope. The outer binding remains unchanged and is accessible again after leaving the inner scope.
Top-level declarations in an ES module are scoped to that module and are not automatically properties of the global object. Modules also have their own strict-mode semantics and explicit import/export boundaries.