Introduction
Every JavaScript program stores information in variables a username, a score, a list of items. But JavaScript actually gives you three different ways to create a variable: var, let, and const. If you're new to JavaScript, it's easy to wonder why there are three keywords instead of one, and which one you should actually use.
This guide explains all three in plain language, with small code examples you can try yourself, so you walk away knowing exactly when to reach for each one.
What is a var (variable)
A variable is just a named box that holds a value. You create one, give it a value and use the name later instead of retyping the value.
let score = 10;
console.log(score);
var:-
var has been in JavaScript since the very beginning. It works, but it has some issues that often confuse beginners which is why it's rarely recommended in modern code.
- Function-scoped: - A var declared inside an if block or a loop “leak” outside of it.
if (true) {
var message = "Hello";
}
console.log(message);
- Can be redeclared: - You can declare the same variable twice with var and JavaScript won't complain which can accidentally overwrite data.
var age = 25;
var age = 30;
3. Hoisted with an initial value of undefined: - JavaScript moves var declarations to the top of their scope, so you can technically use one before the line where it's declared.
console.log(city);
var city = "Ahmedabad";
let: -
let was introduced to fix var's issues. Use it when you need a variable whose value will change later.
1. Block-scoped. A let only exists inside the { } block where it was created.
if (true) {
let message = "Hello";
}
console.log(message); // Reference Error: message is not defined
2. Cannot be redeclared in the same scope. This helps catch mistakes early.
let age = 25;
let age = 30; // Syntax Error: 'age' has already been declared
3. Can be reassigned. You just can't redeclare it — updating the value is completely fine.
let age = 25;
age = 26; // totally fine
const :- For values that don't change
const stands for "constant." Use it by default for any variable you don't plan to reassign — which, in most real code, is most variables.
1. Must be given a value immediately. You can't declare a const without initializing it.
const pi = 3.14; // correct
const radius; // Syntax Error: Missing initializer
2. Cannot be reassigned. Once set, the variable name can't point to a new value.
const pi = 3.14;
pi = 3.15; // Type Error: Assignment to constant variable.
3. But objects and arrays can still change inside. const only locks the variable name, not the contents of an object or array it points to. This trips up a lot of beginners.
const student = {name: "Arpan", year: 3 };
student. year = 4; // fine — we're changing a property, not reassigning "student"
console.log(student); // {name: "Arpan", year: 4}
student = {}; // TypeError: Assignment to constant variable
A classic gotcha: var inside loops
This is one of the most common interview questions about var vs let. Look at this loop that tries to print 0, 1, 2 after a short delay:
for (var i = 0; i < 3; i++) {
set Timeout (() => console.log(i), 100);
}
// Prints: 3, 3, 3 — not what you'd expect!
Because var is function-scoped, all three timeouts share the same i, which finishes the loop and ends up at 3 before any timeout runs. Switching to let fixes it instantly, because let creates a fresh i for each loop iteration:
for (let i = 0; i < 3; i++) {
set Timeout (() => console.log(i), 100);
}
// Prints: 0, 1, 2 — as expected
Var/let/const comparison
| Faeture | Var | Let | Const |
|
Scope |
Function-scoped |
Block-scoped |
Block-scoped |
|
Redeclare in same scope |
Allowed |
Not allowed |
Not allowed |
|
Reassign value |
Allowed |
Allowed |
Not allowed |
|
Must initialize on declaration |
No |
No |
Yes |
|
Hoisting behavior |
Hoisted, set to undefined |
Hoisted, but unusable before declaration |
Hoisted, but unusable before declaration |
Which one should you actually use?
- Use const by default. It's the safest option and makes your code easier to reason about, since the value can't be silently reassigned somewhere else.
- Use let when you know the value needs to change — a loop counter, a running total, a value toggled by user interaction.
- Avoid var in new code. It's still supported for backward compatibility, but block-scoping bugs and accidental redeclaration make it easy to shoot yourself.
Conclusion
var, let, and const all declare variables, but they behave very differently around scope, reassignment, and redeclaration. In modern JavaScript, the simple rule of thumb is: reach for const first, use let when the value must change, and leave var for old codebases you're maintaining rather than new code you're writing
Recent Posts
-
Aug 22 2026
-
Aug 22 2026
-
Aug 22 2026