Understanding JavaScript Variables
When writing programs, we often need to store information like a person’s name, age, or whether someone is a student. JavaScript provides variables to store and manage this information.
Variables are one of the first and most important concepts to understand in programming.
What Are Variables?
A variable is simply a container that stores a value.
A helpful way to think about this is imagining a box that holds information. You give the box a name, and then you can store something inside it.
For example, if we want to store someone’s name:
let name = "Alex";
Here:
nameis the variable"Alex"is the value stored inside it
Later in the program, we can use the variable name whenever we need that value.
Declaring Variables in JavaScript
JavaScript allows us to create variables using three keywords:
varletconst
Example:
var city = "Mumbai";
let age = 21;
const country = "India";
Each of these keywords declares a variable, but they behave slightly differently.
Primitive Data Types
Variables store data, and that data has a type. JavaScript has several primitive data types that beginners should know.
String
Strings represent text.
let name = "Ravi";
Examples of strings:
Names
Messages
Addresses
Number
Numbers represent numeric values.
let age = 22;
Examples:
Age
Price
Score
Boolean
Booleans represent true or false values.
let isStudent = true;
These are often used for conditions or checks.
Null
null represents an intentional empty value.
let data = null;
This means the variable exists but currently holds nothing.
Undefined
undefined means a variable has been declared but no value has been assigned yet.
let score;
console.log(score);
Output:
undefined
Difference Between var, let, and const
At a beginner level, the main difference is whether the value can change.
Example using let:
let age = 20;
age = 21;
console.log(age);
Output:
21
Here the value changed.
Now with const:
const country = "India";
country = "USA";
This will cause an error because const variables cannot be reassigned.
A simple rule to follow:
var → older way of declaring variables
let → used when the value might change
const → used when the value should stay the same
What Is Scope?
Scope describes where a variable can be accessed in the program.
For example:
{
let message = "Hello";
console.log(message);
}
Inside this block, the variable works normally.
But if we try to access it outside:
console.log(message);
It will cause an error because the variable was defined only inside that block.
You can think of scope like a room where a variable exists. Outside that room, the variable cannot be used.
A simple scope visualization could look like:
This shows that the variable belongs only to that block.
Small Practice Exercise
Try this simple example in your browser console.
let name = "Rahul";
let age = 20;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Now try changing the value of age.
Then try the same with const and see what happens.
Conclusion
Variables allow programs to store and manage data. Using var, let, and const, JavaScript lets us create variables that hold different types of values like strings, numbers, and booleans.
Understanding how variables work is an important first step before learning more advanced JavaScript concepts.