When writing JavaScript programs, we often need to store information and use it later. For example, we might need to store a user's name, age, score, or product price.
This is where variables come in.
What is a Variable?
A variable is a named container used to store a value in a program.
For example:
let name = "John";Here, name is a variable that stores the value "John".
We can use that variable later:
let name = "John";
console.log(name);Output:
JohnSo instead of writing "John" everywhere, we can simply use name.
Creating Variables in JavaScript
JavaScript provides three keywords for declaring variables:
letconstvar
For example:
let age = 20;
const country = "India";
var score = 95;All three can create variables, but they behave differently.
Keyword | Can be reassigned? | Can be redeclared in same scope? | Recommended |
|---|---|---|---|
| Yes | No | ✅ Yes |
| No | No | ✅ Yes |
| Yes | Yes | ⚠ Avoid in modern JavaScript |
In modern JavaScript, you'll mostly use let and const.
Using let
Use let when the value of a variable may change later.
let age = 20;
console.log(age);
age = 21;
console.log(age);Output:
20
21Here, we first store 20 in age. Later, we change its value to 21.
Notice that we don't write let again:
age = 21;We only use let when declaring the variable for the first time.
Using const
The const keyword is used when a variable should not be reassigned after it has been created.
const country = "India";
console.log(country);This is allowed:
const country = "India";
console.log(country);But this will cause an error:
const country = "India";
country = "USA";because a const variable cannot be reassigned.
A simple rule to remember is:
Use
constby default, and useletwhen you know the value needs to change.
Using var
var is the older way of declaring variables in JavaScript.
var name = "John";
console.log(name);It still works, but modern JavaScript generally prefers let and const because they provide safer and more predictable behavior.
You'll still see var in older JavaScript code, so it's useful to understand what it means.
Changing Variable Values
Variables declared with let can be changed.
let score = 50;
score = 80;
console.log(score);Output:
80We can also perform calculations:
let price = 100;
price = price + 20;
console.log(price);Output:
120Variable Names
We can choose meaningful names for our variables.
let studentName = "John";
let studentAge = 20;
let examScore = 85;Good variable names make our code much easier to understand.
Rules for Variable Names
There are a few important rules:
1. A variable name can contain letters, numbers, _, and $.
let userName = "John";
let user_1 = "Alex";
let $price = 100;2. A variable name cannot start with a number.
let 1name = "John"; // ❌ InvalidBut this is valid:
let name1 = "John"; // ✅ Valid3. Variable names are case-sensitive.
let name = "John";
let Name = "Alex";These are treated as two different variables.
4. JavaScript reserved keywords cannot be used as variable names.
For example:
let let = 10; // ❌ InvalidVariables Can Store Different Values
A variable isn't limited to storing text. It can store different types of values.
let name = "John";
let age = 20;
let price = 99.99;
let isStudent = true;Here:
namestores text.agestores an integer.pricestores a decimal number.isStudentstores a Boolean value.
We'll learn these different data types in detail in the next topic.
Multiple Variables
We can create multiple variables in the same program:
let name = "John";
let age = 21;
let city = "Delhi";
console.log(name);
console.log(age);
console.log(city);Output:
John
21
DelhiWe can also use variables together:
let name = "John";
let age = 21;
console.log(name + " is " + age + " years old.");Output:
John is 21 years old.let vs const vs var
For modern JavaScript, you can remember it like this:
const → Value should not be reassigned
let → Value needs to change
var → Older JavaScript codeFor example:
const pi = 3.14;
let score = 50;
score = 75;This is the approach you'll commonly use in modern JavaScript programs.
Conclusion
Variables are one of the most basic and important concepts in JavaScript. They allow us to store, access, and work with data in our programs.
In modern JavaScript, you'll mainly work with let and const. Once you understand variables, the next important step is learning what kinds of values those variables can store—which brings us to JavaScript Data Types.