HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Variables

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.


What Is a Variable in JavaScript?

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.


Creating Variables

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 text
  • age stores a number
  • isStudent stores a boolean value
Note: You will learn the full difference between let, const, and var in the next lessons. This page focuses on the general idea of variables.

Using Variables in Statements

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.


Declaring a Variable Without a Value

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".


JavaScript Variables Can Change

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.

Tip: Choose variable names that clearly describe the value they hold. Names like userName, totalPrice, and currentScore are much better than vague names like x or data.

Rules for Variable Names

JavaScript variable names must follow specific rules:

  • A name can contain letters, digits, underscores, and dollar signs
  • A name must begin with a letter, underscore, or dollar sign
  • A name cannot begin with a number
  • A name cannot use reserved JavaScript keywords
  • Variable names are case sensitive
let userName = "Lima";
let _count = 5;
let $price = 100;
Warning: Names like let 2name or let function are invalid. JavaScript will show an error if a variable name breaks the naming rules.

One Statement, Multiple Variables

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.


Summary Table

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

Key Points to Remember

  • Variables store data so it can be reused in your program
  • Modern JavaScript usually uses let and const
  • You can declare a variable first and assign a value later
  • Variables created with let can be reassigned
  • Good variable names make code easier to understand
  • Variable names must follow JavaScript naming rules