Variables in JavaScript
Variables are one of the most important concepts in programming. In JavaScript, a variable is used to store data that can be used later in a program. Instead of writing the same value repeatedly, developers store the value in a variable and reuse it whenever needed.
For example, if a program needs to store a user’s name, age, or score, it can store these values in variables. This makes the code easier to read, maintain, and update.
What is a Variable?
A variable is a named container used to store data. The value stored in a variable can change during the execution of a program.
Think of a variable like a labeled box. You put something inside the box and give the box a name. Whenever you need that item, you refer to the box by its name.
Declaring Variables in JavaScript
In JavaScript, variables can be declared using three keywords:
- var
- let
- const
Each keyword is used to create variables, but they behave slightly differently.
Using var Keyword
The var keyword was traditionally used to declare variables in older versions of JavaScript.
In this example, the variable name stores the value "John". When we print it using console.log(), the value is displayed in the console.
Using let Keyword
The let keyword was introduced in modern JavaScript (ES6). It is now commonly used for declaring variables because it provides better control over variable scope.
Here, the variable age stores the value 25. The value can be changed later in the program if needed.
Using const Keyword
The const keyword is used to declare variables whose values should not change after they are assigned.
Once a value is assigned to a const variable, it cannot be reassigned.
Example of Changing Variable Values
Variables declared with let can be updated during program execution.
Output:
Here the value of score was changed from 10 to 20.
Rules for Naming Variables
When creating variables in JavaScript, certain naming rules must be followed.
- Variable names must begin with a letter, underscore (_), or dollar sign ($)
- Variable names cannot start with a number
- Spaces are not allowed in variable names
- JavaScript keywords cannot be used as variable names
- Variable names are case-sensitive
Example:
Best Practices for Variable Names
Good variable names make the code easier to understand. Developers should choose meaningful names that describe the purpose of the variable.
For example:
- price instead of x
- studentName instead of a
- totalScore instead of num
Using descriptive names helps other developers understand the code more easily.
Conclusion
Variables are fundamental building blocks of JavaScript programming. They allow developers to store and manage data efficiently within a program. JavaScript provides three ways to declare variables: var, let, and const.
Modern JavaScript development mainly uses let and const because they provide better control and safer code structure. Understanding how variables work is essential before moving on to more advanced topics like functions, objects, and data manipulation.
In the next tutorial, you will learn about JavaScript Data Types and how different kinds of data are stored and used in variables.

