JavaScript variables are containers used to store data. Instead of repeating the same value again and again, you can save it in a variable and use that name whenever you need it. This makes your code easier to read, update, and reuse.
A variable is a named storage location for data. That data can be a number, text, a true or false value, or more complex information. Variables help JavaScript remember values while your program runs.
Variables are important because they let you store data once and work with it again and again. For example, a variable can hold a user's name, a product price, a total score, or a message shown on the page. Instead of hardcoding the same value many times, you can keep it inside a variable and update it in one place when needed.
This makes your code easier to manage, easier to read, and much more flexible as your programs grow.
In modern JavaScript, variables are commonly created using let and
const. You may also see var in older code. For now, the main
idea is simple: a variable has a name and usually a value.
let name = "Amina";
let age = 20;
let isStudent = true;
In this example:
name stores textage stores a numberisStudent stores a boolean valuelet,
const, and var in the next lessons. This page focuses on the
general idea of variables.
Once a value is stored in a variable, you can use that variable in calculations, messages, and page updates.
let price = 50;
let quantity = 3;
let total = price * quantity;
console.log(total);
Here, JavaScript multiplies the values stored in price and
quantity, then stores the result in total.
You can create a variable first and assign its value later. This is useful when you know the name you want to use, but the value will be added afterward.
let city;
city = "Dhaka";
At first, city exists without a value. After the second line, it stores the
string "Dhaka".
Variables created with let can receive a new value later in the code. This
is one reason they are called variables: the stored value can vary.
let score = 10;
score = 15;
console.log(score);
After reassignment, the value of score becomes 15.
userName, totalPrice, and currentScore
are much better than vague names like x or data.
JavaScript variable names must follow specific rules:
let userName = "Lima";
let _count = 5;
let $price = 100;
let 2name or
let function are invalid. JavaScript will show an error if a variable name
breaks the naming rules.
You can declare multiple variables in one statement, although many developers prefer one variable per line because it is easier to read.
let x = 5, y = 6, z = x + y;
This is valid, but using separate lines is often cleaner in beginner-friendly code.
| Concept | Meaning |
|---|---|
| Variable | A named container for storing data |
| Declaration | Creating a variable using let, const, or var |
| Assignment | Giving a value to a variable |
| Reassignment | Updating a variable with a new value later |
| Identifier | The name of the variable |
let and constlet can be reassigned