# Declaring variables with var, let and const

*JavaScript Developer Study Guide · Variables, Types, and Collections · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/javascript-developer/variables-types-and-collections/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.

- Certification: Salesforce JavaScript Developer
- Exam section: Variables, Types, and Collections
- Study guide: https://www.certifyforce.com/study-guide/javascript-developer

**Official exam objective:**

> Given a scenario, write code to create variables and initialize them correctly.

## 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`, `let` or `const`.
- Choose the keyword that lets the code run: `let` for values assigned later, `const` with a single-expression initializer otherwise.
- Explain why `const` objects and arrays can still be changed, and what `Object.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** (`var` declarations processed before any code runs); **temporal dead zone (TDZ)** (from the start of the scope until a `let`/`const` line runs, reading the name throws `ReferenceError`).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| `var` scope | Function (or script/module); blocks do not limit it | [var](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) |
| `let`/`const` scope | Nearest enclosing block | [Grammar and types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types) |
| `var` read before its line | `undefined` (declaration hoisted, value not) | [Grammar and types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types) |
| `let`/`const` read before its line | `ReferenceError`, even with `typeof` (TDZ) | [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) |
| `const x;` | `SyntaxError: Missing initializer in const declaration` | [const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) |
| Reassigning a `const` | `TypeError: Assignment to constant variable` | [const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) |
| Duplicate `let` in one scope | Parse-time `SyntaxError`; nothing in the script runs | [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), [SyntaxError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) |
| Duplicate `var` | Allowed, even in strict mode | [var](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) |
| Undeclared assignment | Sloppy: creates a global; strict: `ReferenceError` | [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) |
| `Object.freeze()` | Shallow; sloppy writes fail silently, strict writes throw | [Object.freeze()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) |
| `let` in a `for` header | New binding each iteration | [for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) |
| Destructuring default | Used for missing or `undefined`, not `null` | [Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring) |

## Check yourself

- Why does `console.log(x); var x = 3;` log `undefined` while the same line with `let` throws?
- When must you use `let` instead of `const` for a value chosen in an `if...else`?
- What does `let a = b = 5;` inside a sloppy-mode function do to `b`?
- Why does `let i` in a `for` header fix callbacks that all log the final value?
- Which value does `const { lang = 'en' } = { lang: null }` give `lang`?

## Sources

- [Grammar and types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types)
- [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)
- [const](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)
- [var](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
- [for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring)
- [Object.freeze()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
- [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
- [SyntaxError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
- [Primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 12 linked practice questions, and unlimited timed mock exams are included with the JavaScript Developer pack (USD $19): https://www.certifyforce.com/practice-exams
