What is the difference between const, var, and let in JavaScript?
There are three ways to declare variables in JavaScript: const, var, and let. But what is the difference between them?


What is the difference between const var and let in JavaScript?
Understanding variable declarations is crucial in JavaScript. Let's explore the three ways to declare variables: const
, var
, and let
, and understand their key differences and use cases.
What is const in JavaScript?
const
is used to declare constants - variables whose values cannot be reassigned after initialization. This makes your code more predictable and helps prevent accidental changes.
1const name = "John";2name = "Jane"; // TypeError: Assignment to constant variable34// However, for objects and arrays:5const user = { name: "John" };6user.name = "Jane"; // This works - only reassignment is prevented
const
variables are block-scoped, meaning they are only accessible within the block they are declared in (like within if statements or loops).
What is var in JavaScript?
var
is the original way to declare variables in JavaScript. It has some unique behaviors that can sometimes lead to unexpected results.
1var name = "John";2name = "Jane"; // This works fine34// Function scope example5function example() {6 var x = 1;7 if(true) {8 var x = 2; // Same variable!9 console.log(x); // 210 }11 console.log(x); // 212}1314// Hoisting example15console.log(hoisted); // undefined16var hoisted = "I am hoisted";
var
is function-scoped rather than block-scoped, and it gets hoisted to the top of its scope. These behaviors can make code harder to understand and maintain, which is why modern JavaScript tends to avoid var
.
What is let in JavaScript?
let
provides a more predictable way to declare variables that need to be reassigned. It combines the flexibility of var
with better scoping rules.
1let count = 1;2count = 2; // This works fine34// Block scope example5function example() {6 let x = 1;7 if(true) {8 let x = 2; // Different variable9 console.log(x); // 210 }11 console.log(x); // 112}1314// No hoisting behavior15console.log(notHoisted); // ReferenceError16let notHoisted = "I am not hoisted";
When should you use const var or let?
Here are some guidelines for choosing between these declarations:
-
Use
const
by default- For values that won't be reassigned
- For object and array references
- Makes code intentions clearer
-
Use
let
when you need to reassign- For counters in loops
- For values that change over time
- When working with mutable state
-
Avoid
var
in modern code- Legacy code still uses it
- Can lead to scope confusion
- Lacks block scoping benefits
Summary
const
: For values that won't be reassigned, block-scopedvar
: Legacy declaration, function-scoped, hoistedlet
: For reassignable values, block-scoped, no hoisting
Looking for more?
Ready to test your understanding of variable declarations in JavaScript? Try our practice challenges below!
How to Create a Personalized Greeting Message Using JavaScript String Concatenation
JavaScript While Loop Not Incrementing Counter Variable - Infinite Loop Issue
How Does Variable Reassignment Work in JavaScript: Number to String Conversion
Land your dream tech job faster
Join 810+ developers who are accelerating their coding skills with TechBlitz.