JavaScript Variables
Understanding JavaScript variables is one of the most important steps in learning programming. Variables allow us to store, reuse, and modify data while building real-world applications.
In this lesson, you’ll learn:
- What is a variable in JavaScript
- How memory works with variables
- Difference between var, let, and const
- Scope, redeclaration, and reassignment explained clearly
- Best practices for modern JavaScript

What Is a Variable in JavaScript?
A variable is a container used to store data values so they can be reused and modified later.
- Data is stored in memory
- A variable is a name that refers to a memory location
- That name allows us to access and update the stored value
Example
let name = "Rohan";Here:
- name → Variable name
- “Rohan” → Value stored in memory
Whenever we use name, JavaScript retrieves the value stored in that memory location.
Ways to Declare Variables in JavaScript
JavaScript provides three keywords to declare variables:
- var (Old method)
- let (Modern variable)
- const (Constant value)
var – The Old Way of Declaring Variables
var was the original keyword used to declare variables before ES6 (2015).
Although it still works, it is not recommended in modern JavaScript development because of several design problems.
Example
var age = 25;Scope of var
var Is Not Block-Scoped
A block means code written inside { }, such as in:
- if
- for
- while
- switch
Variables declared with var ignore block scope.
Example
if (true) {
var x = 10;
}
console.log(x); // 10Even though x is declared inside the block, it is accessible outside the block. This behavior can cause logical errors and unexpected bugs.
var Is Function-Scoped
If var is declared inside a function, it stays inside that function.
function test() {
var y = 20;
}
console.log(y); // Error: y is not definedSo:
- var respects function scope
- var ignores block scope
Redeclaration Problem
Variables declared using var can be redeclared in the same scope.
var x = 10;
var x = 20; // Allowed
console.log(x); // 20Problems:
- No warning from JavaScript
- Previous value gets overwritten silently
- Can introduce hidden bugs
Reassignment Is Allowed
var count = 5;
count = 8; // AllowedUpdating is normal, but combined with redeclaration, it becomes risky.
let – The Modern Way to Declare Variables
“let” was introduced in ES6 (2015) to fix the problems of var.
It should be used when:
- The value may change later
- You want proper block scope
Example
let city = "Delhi";
city = "Mumbai"; // AllowedHere, the variable city is updated safely.
let Is Block-Scoped
A variable declared with let exists only inside the block where it is declared.
if (true) {
let x = 10;
}
console.log(x); // Error: x is not definedThis is correct and expected behavior.
Difference Between var and let (Scope)
if (true) {
var a = 5;
let b = 10;
}
console.log(a); // 5
console.log(b); // Error- var ignores block scope
- let strictly follows block scope
This makes let safer and easier to manage.
Redeclaration Not Allowed
let a = 5;
let a = 10; // ErrorThis prevents accidental overwriting.
Redeclaration in Different Block Is Allowed
let x = 5;
if (true) {
let x = 10; // Different block, allowed
}Each block has its own separate scope.
const – For Constant Values
const is used for values that should not change.
Example
const country = “India”;
Characteristics of const
- Block-scoped
- Cannot be redeclared
- Cannot be reassigned
const pi = 3.14;
// pi = 3.14159; ErrorDifference Between var, let, and const
| Feature | var | let | const |
| Scope | Function | Block | Block |
| Redeclare | Yes | No | No |
| Reassign | Yes | Yes | No |
| Modern Usage | No | Yes | Yes |
Best Practice in Modern JavaScript
- Use const by default
- Use let if the value needs to change
- Avoid var in modern development