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.
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
varfunction expression or alet/constbinding is used before its line, includingtypeofchecks. - Read closures that capture variables: factories with independent state, counters, and closures that see later reassignments.
- Trace execution order through nested calls, recursion,
return,switchfall-through,breakandcontinue. - 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
- Source
- var (opens in a new tab)
Top-level
let- Detail
- No property on
globalThis - Source
- let (opens in a new tab)
switch body- Detail
- One block shared by all cases
- Source
- let (opens in a new tab)
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)
- Scopedeveloper.mozilla.org (opens in a new tab)
- Functions (JavaScript Guide)developer.mozilla.org (opens in a new tab)
- functiondeveloper.mozilla.org (opens in a new tab)
- Grammar and typesdeveloper.mozilla.org (opens in a new tab)
- Hoistingdeveloper.mozilla.org (opens in a new tab)
- typeofdeveloper.mozilla.org (opens in a new tab)
- Strict modedeveloper.mozilla.org (opens in a new tab)
- vardeveloper.mozilla.org (opens in a new tab)
- letdeveloper.mozilla.org (opens in a new tab)
- switchdeveloper.mozilla.org (opens in a new tab)
- Closuresdeveloper.mozilla.org (opens in a new tab)
- 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