# Variables & Scope

> How and where values are stored in memory. Master let, const, var, and the rules of block versus function scope.

Subject: JavaScript | Lesson 2 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/variables-and-scope
Previous: [How JavaScript Runs](https://codewithanimation.in/javascript/how-js-runs.md) | Next: [Data Types & Primitives](https://codewithanimation.in/javascript/data-types-and-primitives.md)

## What are variables and scope?

A variable is a named container for storing data in memory. In JavaScript, you declare a variable using `let`, `const`, or the legacy `var` keyword.

Scope is the set of rules that determines where a variable is accessible in your code. Think of it as a one-way mirror: inner code blocks can see out to outer variables, but outer code blocks cannot see in.

## Why does it matter?

Choosing the right keyword prevents unexpected bugs. `const` guarantees the variable identifier will not be reassigned, making your code predictable.

Understanding scope is critical for keeping data private and avoiding collisions, where two parts of your program accidentally overwrite the same variable name.

## How it works

`const` and `let` are block-scoped, meaning they only exist within the nearest curly braces `{}` (like an `if` statement or `for` loop).

`var` is function-scoped. If it is not inside a function, it leaks out and becomes a global variable, which is a common source of bugs.

When a variable is declared but not yet assigned, `let` and `const` sit in a Temporal Dead Zone (TDZ). Accessing them before initialization throws a `ReferenceError`. By contrast, `var` simply returns `undefined`.

## Common mistake: The Temporal Dead Zone (TDZ) trap

If you try to read a `let` or `const` variable before the line of code that declares it, JavaScript throws a ReferenceError. The variable is in the Temporal Dead Zone from the start of the block until the declaration is executed.

## Tip: Default to const

Always use `const` by default. Only switch to `let` if you know you need to reassign the value later (like a loop counter). Never use `var` in modern JavaScript.

## Try it: Try it out

Declare variables inside and outside a block, then try to access them.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/variables-and-scope

## Check yourself

### Question 1

What happens if you try to access a `let` variable before its declaration?

- A. It throws a ReferenceError.
- B. It returns undefined.
- C. It returns null.

Correct answer: A

Accessing a let or const variable before its declaration hits the Temporal Dead Zone (TDZ), throwing a ReferenceError.

- Why not B: Incorrect. This is what happens when you hoist a `var` declaration, but not `let` or `const`.
- Why not C: Incorrect. JavaScript never defaults an uninitialized variable to null.

### Question 2

Which of the following keywords is block-scoped?

- A. let
- B. var
- C. function

Correct answer: A

Both let and const are block-scoped, meaning they are confined to the nearest set of curly braces.

- Why not B: Incorrect. `var` is function-scoped, ignoring block boundaries like if statements.
- Why not C: Incorrect. While functions create scope, the keyword itself does not declare a block-scoped variable.

### Question 3

Can you mutate an object declared with `const`?

- A. Yes, const only prevents reassignment of the identifier.
- B. No, const makes the value completely immutable.
- C. Only if the object is empty.

Correct answer: A

const means the binding cannot be changed, but the contents of an object or array can still be modified (mutated).

- Why not B: Incorrect. To make an object fully immutable, you must use Object.freeze().
- Why not C: Incorrect. The state of the object does not change how const behaves.

## Remember this

- `const` prevents reassignment, not mutation of objects.
- `let` and `const` are block-scoped (`{ }`).
- `var` is function-scoped and hoists as `undefined`.
- Accessing `let`/`const` before declaration triggers the TDZ.
