01 / 20
{ } JS

var vs. let/const

Understand how JavaScript's three declaration keywords differ in scope, hoisting, and reassignment: the foundation of writing predictable, bug-free code.

JavaScript

Why Variable Declarations Matter

Every JavaScript program stores data in variables. The keyword you choose to declare a variable controls where that variable is visible, when it exists, and whether it can be changed.

Picking the wrong keyword is one of the most common sources of subtle bugs: values that "leak" out of loops, variables that seem to exist before they're assigned, and constants that accidentally get overwritten.

What Is Scope?

Scope is the region of your code where a variable is accessible. Think of it as the variable's "visibility boundary." Outside that boundary, the variable doesn't exist.

Function Scope
The variable is visible anywhere inside the function it was declared in, but not outside.
Block Scope
The variable is visible only inside the nearest { } block: an if-statement, loop, or any curly-brace pair.

What Is Hoisting?

Before your code runs, JavaScript "hoists" (moves) declarations to the top of their scope. But the three keywords are hoisted differently.

var hoisting
Declaration and initialization to undefined are hoisted. You can read the variable before its line: you'll get undefined instead of an error.
let / const hoisting
Declaration is hoisted, but the variable stays in a "Temporal Dead Zone" (TDZ). Accessing it before its line throws a ReferenceError.

Scope Visualized

FUNCTION SCOPE var x = 10; // visible throughout function BLOCK SCOPE (if) let y = 20; // visible only in this block const z = 30; // visible only in this block var w = 40; // escapes block! visible in function console.log(x); // 10 ✓ console.log(y); // ReferenceError: y is not defined console.log(w); // 40 ✓ (var escapes block)

The Three Core Rules

1
Scope: var is function-scoped. let and const are block-scoped.
2
Hoisting: var is hoisted with the value undefined. let/const are hoisted but stay in the Temporal Dead Zone until their declaration line.
3
Reassignment: var and let can be reassigned. const cannot be reassigned after initialization (but object properties can still change).

Hoisting in Action

time var x = 5 Hoisted: var x = undefined console.log(x); // undefined var x = 5; // now x = 5 let y = 5 TEMPORAL DEAD ZONE console.log(y); // Error! let y = 5; // now y = 5 After declaration, both behave the same const z = { } z = { a: 1 }; // TypeError: Assignment to constant z.prop = "OK"; // OK! Object properties can change

let vs. const: When to Use Which

Use const by default
If a variable's binding never changes (you never write x = something_new), use const. It signals intent: "this name always points to the same value."
Use let when you need reassignment
Counters in loops, accumulators, values that swap: use let. It signals: "this name will point to different values over time."

How to Choose the Right Keyword

1
Start with const. Declare every new variable with const first.
2
Need to reassign? If you later write x = newValue, change const to let.
3
Never reach for var. There is no modern scenario where var is the better choice.
4
Declare close to usage. Thanks to block scope, declare variables right where they're needed: inside loops, inside if-blocks, etc.

Example 1: var Leaks Out of Blocks

1function greet() { 2 if (true) { 3 var message = "hello"; 4 } 5 console.log(message); 6} 7greet(); // "hello" — var escapes the if-block!
1function greet() { 2 if (true) { 3 let message = "hello"; 4 } 5 console.log(message); // ReferenceError! 6}

Example 2: The Classic Loop Bug

1for (var i = 0; i < 3; i++) { 2 setTimeout(function() { 3 console.log(i); 4 }, 100); 5} 6// Output: 3, 3, 3 (not 0, 1, 2!)
1for (let i = 0; i < 3; i++) { 2 setTimeout(function() { 3 console.log(i); 4 }, 100); 5} 6// Output: 0, 1, 2 ✓

Example 3: Hoisting Surprises

1console.log(a); // undefined (no error!) 2console.log(b); // ReferenceError 3 4var a = 10; 5let b = 20;

JavaScript internally rewrites the var code as if it were:

1var a; // hoisted to top, set to undefined 2console.log(a); // undefined 3a = 10; // actual assignment stays in place

const Does Not Mean Immutable

const prevents reassignment of the variable binding. It does not freeze the value itself. If the value is an object or array, its contents can still change.

1const user = { name: "Alice" }; 2 3user.name = "Bob"; // ✓ OK: mutating property 4user.age = 25; // ✓ OK: adding property 5 6user = { name: "Eve" }; // ✕ TypeError: reassignment!

Common Mistakes

✕ Using var in loops
Loop counter leaks into the surrounding function. Use let instead: each iteration gets its own copy of the counter.
✕ Thinking const = immutable
const only prevents reassignment. Object properties and array elements can still be modified. Use Object.freeze() for true immutability.
✕ Relying on var hoisting
Using a var variable before its declaration line "works" (returns undefined), but this hides bugs. let/const throw an error, which is safer.

Side-by-Side Comparison

Feature var let const
Scope Function Block Block
Hoisting Yes (undefined) Yes (TDZ) Yes (TDZ)
Reassignment ✓ ✓ ✕
Redeclaration ✓ ✕ ✕
Use before declare Returns undefined ReferenceError ReferenceError
Best practice Avoid When reassigning Default choice

Question 1 of 5

What type of scope does var have in JavaScript?

Question 2 of 5

What happens when you try to access a let variable before its declaration line?

Question 3 of 5

What is the output of the code above?

Question 4 of 5

What will be logged to the console?

Question 5 of 5

Which statement best describes the modern best practice for variable declarations in JavaScript?