Skip to solution
easyPhone Screen

Describe the concept of 'scope' in JavaScript.

795 views
01

Understand the problem

Question presented to candidate: "Explain what scope means in JavaScript, and walk me through the practical difference between how var, let, and const each decide where a variable lives."

What a strong answer should cover:

  • Scope is the region of code where a given variable name can be looked up; a name lookup walks outward through enclosing scopes (the scope chain) but never inward.
  • var is function-scoped (or global-scoped at the top level) — it ignores block boundaries like if and for, so a var declared inside a block is still visible after that block ends.
  • let and const are block-scoped — confined to the nearest enclosing curly-brace block.
  • let and const are hoisted but sit in the temporal dead zone until their declaration line executes, so reading them earlier throws a ReferenceError; var is accessible (as undefined) before its own declaration line runs.
  • The classic var-in-a-loop-closure bug: closures built inside a var-based for loop all share one variable and see its final value; a let-based loop gives each closure its own per-iteration binding.

Clarifying questions expected:

  • "Should I also cover the temporal dead zone, or just the block-versus-function distinction?"
  • "Is this specifically about lexical scope, or should I also touch on this-binding, which is a separate mechanism?"

Code / implementation expected: Yes — a short, runnable snippet demonstrating var leaking out of a block, the TDZ throwing on early access, and the classic var-vs-let loop-closure difference.

scopevariablesfunctionsglobal
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is re

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSScope in practice: var leaking past a block, the TDZ, and the var-vs-let loop-closure bug (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 14 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track