What is a Variable?
A variable is a named container used to store data in a program. The stored value can be a number, string, object, array, or any other data type.
Example:
let name = "John";
let age = 25;
1. var
var is the oldest way to declare variables in JavaScript. It was used before the introduction of ES6 (ECMAScript 2015).
Syntax
var city = "Chennai";
Characteristics
- Function-scoped
- Can be redeclared
- Can be reassigned
- Hoisted and initialized with
undefined
Example
var language = "JavaScript";
console.log(language);
language = "Python";
console.log(language);
var language = "Java";
console.log(language);
Output:
JavaScript
Python
Java
Scope Example
if (true) {
var message = "Hello";
}
console.log(message);
Output:
Hello
Although message is declared inside the if block, it is still accessible outside because var is function-scoped, not block-scoped.
2. let
let was introduced in ES6 and is now the preferred way to declare variables whose values may change.
Syntax
let score = 90;
Characteristics
- Block-scoped
- Cannot be redeclared in the same scope
- Can be reassigned
- Hoisted but not initialized (Temporal Dead Zone)
Example
let marks = 80;
marks = 95;
console.log(marks);
Output:
95
Block Scope Example
if (true) {
let number = 10;
console.log(number);
}
console.log(number);
Output:
10
ReferenceError: number is not defined
The variable exists only inside the block where it is declared.
3. const
const is also introduced in ES6 and is used for values that should not be reassigned.
Syntax
const PI = 3.14159;
Characteristics
- Block-scoped
- Cannot be redeclared
- Cannot be reassigned
- Must be initialized during declaration
Example
const country = "India";
console.log(country);
Attempting to reassign it:
country = "USA";
Output:
TypeError: Assignment to constant variable.
Objects with const
A const object cannot be reassigned, but its properties can be modified.
const student = {
name: "Alex",
age: 20
};
student.age = 21;
console.log(student);
Output:
{
name: "Alex",
age: 21
}
Difference Between var, let, and const
| Feature | var |
let |
const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Redeclaration | Yes | No | No |
| Reassignment | Yes | Yes | No |
| Hoisted | Yes | Yes | Yes |
| Initialized During Hoisting | Yes (undefined) |
No | No |
| Must Initialize | No | No | Yes |
Hoisting Example
console.log(a);
var a = 5;
Output:
undefined
console.log(b);
let b = 5;
Output:
ReferenceError
console.log(c);
const c = 5;
Output:
ReferenceError
When Should You Use Each?
Use const when:
- The variable value should not change.
- Declaring constants like API URLs or configuration values.
Example:
const company = "OpenAI";
Use let when:
- The value needs to change later.
Example:
let count = 0;
count++;
console.log(count);
Avoid var in modern JavaScript because:
- It ignores block scope.
- It allows accidental redeclaration.
- It can introduce bugs in large applications.
Best Practices
- Use
constby default. - Use
letonly when the value needs to change. - Avoid using
varin modern JavaScript development. - Give variables meaningful names.
- Keep variable scope as small as possible.
Conclusion
Understanding the differences between var, let, and const is essential for writing reliable JavaScript code. While var was widely used in older JavaScript versions, modern development favors let and const because they provide block scope and help prevent common programming mistakes.
As a general rule:
- Use
constfor values that should remain unchanged. - Use
letfor variables whose values will change. - Avoid
varunless you're maintaining legacy JavaScript code.
Top comments (0)