If you’ve just started learning JavaScript, one thing you’ve probably come across are the keywords let, const, and var. They all seem to do something similar — they help you declare variables — but they’re not exactly the same.
So, what’s the difference? And when should you use each one?
1. What Are These Keywords?
In JavaScript, when you want to store a value like a number, a string, or even an object, you use a variable. And to create that variable, you use one of these three keywords:
var
let
const
All three are used to declare variables, but they behave differently in how they store, scope, and update the values.
2. var – The Old School Way
Let’s start with var. It’s the oldest way to declare a variable in JavaScript. People used it before let and const even existed.
var name = "ABC";
console.log(name); // Output: ABC
Seems fine, right?
But here’s the thing: var is function-scoped, not block-scoped. That means if you declare a var inside a block like an if statement or a loop, it’s still available outside that block — which can lead to bugs.
Example:
if (true) {
var age = 30;
}
console.log(age); // Output: 30 — even though age was inside the if block!
This can be confusing. That’s why let and const were introduced in newer versions of JavaScript (ES6).
- let – The Modern, Flexible Option Now let’s talk about let. This one is block-scoped. In simple words, if you declare a variable using let inside a block, it stays there and won’t leak outside. if (true) { let city = "XYZ"; console.log(city); // Output: XYZ } console.log(city); // Error! city is not defined outside the block
This is safer and more predictable. You use let when you want to change or update the value of a variable later.
Example:
let counter = 1;
counter = 2; // Totally fine
console.log(counter); // Output: 2
So, if you plan to reassign a variable, go with let.
4. const – For Things That Never Change
As the name suggests, const stands for constant. Once you set a value using const, you can’t change it.
const country = "India";
country = "USA"; // Error! You can’t reassign a const
Just like let, const is also block-scoped, which is great. You should use const by default when you don’t need to change the value of the variable. It makes your code easier to understand.
A Small Twist:
Even though you can’t reassign a const, if the value is an object or array, you can change the contents.
const user = { name: "ABC" };
user.name = "QWE"; // This is allowed!
console.log(user); // Output: { name: "QWE" }
So, the reference stays constant, but the inside values can be changed.
6. Which One Should You Use?
Here’s a simple rule of thumb:
Use const most of the time.
Use let if you know the value will change.
Avoid var unless you're working w ith old JavaScript code.
7. Final Thoughts
Understanding the difference between let, const, and var helps you write cleaner, safer, and more predictable code. If you’re new to JavaScript, stick with let and const. They’re easier to manage and better suited for today’s coding standards.
And remember — even experienced developers make mistakes with these sometimes. The more you practice, the more natural it’ll feel.
Happy coding!
Top comments (0)