Variables, Types, and Collections23% of the exam
Declaring variables with var, let and const
How var, let and const differ in scope, hoisting, the temporal dead zone, re-declaration and reassignment, why const does not freeze objects, and the var-in-a-loop closure trap the exam loves.
Given a scenario, write code to create variables and initialize them correctly.
~7 min read · 12 practice questions
What the exam expects
- Predict what a snippet logs or throws when a variable is read before, inside or after the block that declares it with
var,letorconst. - Choose the keyword that lets the code run:
letfor values assigned later,constwith a single-expression initializer otherwise. - Explain why
constobjects and arrays can still be changed, and whatObject.freeze()adds (and does not). - Fix the classic
var-in-a-loop closure bug, and spot implicit globals and duplicate declarations. - Initialize several variables at once with destructuring, including renames, nesting and defaults.
- Key terms: declaration (
let x, makes the name usable); initializer (= 42, the first value); block scope (visible only inside the nearest{ }); hoisting (vardeclarations processed before any code runs); temporal dead zone (TDZ) (from the start of the scope until alet/constline runs, reading the name throwsReferenceError).
Key facts
var scope- Detail
- Function (or script/module); blocks do not limit it
- Source
- var (opens in a new tab)
let/const scope- Detail
- Nearest enclosing block
var read before its line- Detail
undefined(declaration hoisted, value not)
let/const read before its line- Detail
ReferenceError, even withtypeof(TDZ)- Source
- let (opens in a new tab)
const x;- Detail
SyntaxError: Missing initializer in const declaration
Reassigning a
const- Detail
TypeError: Assignment to constant variable
Duplicate
let in one scope- Detail
- Parse-time
SyntaxError; nothing in the script runs
Duplicate
var- Detail
- Allowed, even in strict mode
- Source
- var (opens in a new tab)
Undeclared assignment
- Detail
- Sloppy: creates a global; strict:
ReferenceError
Object.freeze()- Detail
- Shallow; sloppy writes fail silently, strict writes throw
let in a for header- Detail
- New binding each iteration
- Source
- for (opens in a new tab)
Destructuring default
- Detail
- Used for missing or
undefined, notnull
Sources for this section (9)
- vardeveloper.mozilla.org (opens in a new tab)
- Grammar and typesdeveloper.mozilla.org (opens in a new tab)
- letdeveloper.mozilla.org (opens in a new tab)
- constdeveloper.mozilla.org (opens in a new tab)
- SyntaxErrordeveloper.mozilla.org (opens in a new tab)
- Strict modedeveloper.mozilla.org (opens in a new tab)
- Object.freeze()developer.mozilla.org (opens in a new tab)
- fordeveloper.mozilla.org (opens in a new tab)
- Destructuringdeveloper.mozilla.org (opens in a new tab)
- Why does
console.log(x); var x = 3;logundefinedwhile the same line withletthrows? - When must you use
letinstead ofconstfor a value chosen in anif...else? - What does
let a = b = 5;inside a sloppy-mode function do tob? - Why does
let iin aforheader fix callbacks that all log the final value? - Which value does
const { lang = 'en' } = { lang: null }givelang?
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
- Practice exams purchase adds inline bank questions and topic practice (12 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Choose guide-only, practice-only, or save $4 with both