Skip to content
BasicsChapter 4 / 47Beginner5 min read

Variables: let, const, var

Learn the difference between let, const and var in JavaScript, block scope, reassignment and when to use each one, with modern examples.

On this page

1. What are variables?

JavaScript has three keywords for creating variables:

KeywordIntroducedStatus today
constES2015The default choice
letES2015When the value needs to change
varFrom the startThe old way, avoid it in new code

2. Why does it matter?

Without variables you would have to write every value again and again. Giving names makes code readable, and changing a value in one place updates the whole program.

Old var styleModern let and const
Function scope, leaks outside the blockBlock scope, only inside the curly braces
The same name can be declared againDeclaring again throws a SyntaxError
Using before declaring gives undefinedUsing before declaring gives a ReferenceError
Every value can changeconst tells you the value will not change

3. Where is it used?

SituationKeywordExample
API URL, configconstconst API_URL = "/api/users"
Counter, totalletlet total = 0
DOM element referenceconstconst btn = document.querySelector("button")
Loop counterletfor (let i = 0; i < 5; i++)
Array or object that changes insideconstconst cart = [] then cart.push(item)

4. How does it work?

A variable's life has three steps: declare, assign, use.

A variable's journey
Declare (let score)
Assign (score = 10)
Use (console.log(score))
Reassign, only with let (score = 20)

Scope means how far a variable is visible. let and const are block scoped, which means they are limited to the curly braces block. var is function scoped and escapes outside an if or for block too.

One more concept: before they are declared, let and const are in the Temporal Dead Zone (TDZ). Accessing them at that time throws an error. The details are in Scope & Hoisting.

5. Code example

js
const appName = "ZnPedia";
let visitors = 0;

visitors = visitors + 1;
visitors += 1;

console.log(appName, visitors);

With const, changing something inside an object is allowed, but assigning a new value is not:

js
const user = { name: "Sara", age: 21 };

user.age = 22;
console.log(user.age);

user = { name: "Ali" };

The last line will throw TypeError: Assignment to constant variable.

The difference in block scope:

js
if (true) {
  var oldWay = "visible outside too";
  let newWay = "inside only";
}

console.log(oldWay);
console.log(newWay);

The first log will work, the second will throw ReferenceError: newWay is not defined.

How does the code work?

ItemMeaning
const appNameThe value cannot be assigned again
let visitorsThe value can change, updated with +=
user.age = 22Changing a property inside an object is allowed
user = ...Assigning a new value is not allowed with const
var oldWayLeaks outside the block
let newWayStays inside the block

Interview tip

"Does const make an object immutable?" No. const only locks the binding, meaning the variable cannot point to some other value. To freeze an object completely, use Object.freeze().

6. Common mistakes

Mistake: Writing let everywhere

Why? The reader cannot tell which values will change, and there is a chance of reassigning by accident.

Better: Write const by default, and use let only when the value really needs to change.

Mistake: Using var in new code

Why? var ignores block scope and, because of hoisting, causes strange bugs like undefined.

Better: Use let or const instead of var. Understand var only so you can read old code.

Mistake: Thinking const is fully immutable

Why? Pushing to a const array or changing an object's property still works, which causes confusion.

Better: Remember that const only prevents reassignment. To freeze data, use Object.freeze.

Mistake: Using a variable before declaring it

Why? let and const are in the TDZ, so a ReferenceError is thrown.

Better: Declare variables at the top, before you use them.

7. What to learn before and after

StageTopics
BeforeSetup & First Program
After thisData Types, Operators
Going deeperScope & Hoisting, Closures

Final quick revision

PointDetail
constDefault choice, cannot be reassigned
letFor values that change, block scoped
varOld, function scoped, avoid it
Block scopeLimited to inside the curly braces
TDZAccessing let / const before declaring throws an error