Objects, Functions, and Classes25% of the exam

Scope, hoisting and closures

How JavaScript resolves a name through the lexical scope chain, what hoisting does to function declarations, var and let, how closures keep variables alive, and how calls, returns, recursion, switch fall-through and break/continue decide the order of output.

Official exam objective
Given a block of code, analyze the variable scope and the execution flow.

~7 min read · 17 practice questions ·

What the exam expects

  • Trace which variable a name resolves to through nested and shadowing scopes, and which names are visible at a given line.
  • Predict what happens when a function declaration, a var function expression or a let/const binding is used before its line, including typeof checks.
  • Read closures that capture variables: factories with independent state, counters, and closures that see later reassignments.
  • Trace execution order through nested calls, recursion, return, switch fall-through, break and continue.
  • Tell which top-level declarations become global object properties in a classic script versus a module.
  • Key terms: lexical scope (a function sees the scopes where it is written, not where it is called); scope chain (the inside-out list of scopes searched for a name); shadowing (an inner declaration hiding an outer one with the same name); hoisting (declarations taking effect before their line runs); closure (a function plus the variables of the scope where it was created); call stack (last-in-first-out frames of running functions).

Key facts

Lookup direction
Detail
Inner scopes see outer ones, never the reverse
Name conflicts
Detail
The innermost declaration wins
Function declaration
Detail
Hoisted with its body; callable anywhere in scope
var before its line
Detail
Reads undefined
let/const/class before their line
Detail
ReferenceError (temporal dead zone)
typeof undeclared name
Detail
"undefined", no error
Undeclared assignment in strict mode
Detail
Throws instead of creating a global
Top-level var in a classic script
Detail
Becomes a global object property; not in modules
Top-level let
Detail
No property on globalThis
switch body
Detail
One block shared by all cases
Missing break
Detail
Falls through to later cases, including default
Factory closures
Detail
Each call has its own lexical environment
Sources for this section (11)
  • Why does a function returned from `outer()` still read `outer`'s variable when called from another function with a same-named variable?
  • When does `typeof x` throw?
  • Why does calling a `var` function expression above its line throw a TypeError rather than a ReferenceError?
  • What does a `switch` do after a matching case that has no `break`?
  • Which declarations at the top of a classic script become properties of `window`?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the JavaScript Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 24 lessons of the JavaScript Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (17 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the JavaScript Developer exam