Understanding Variables and Data Types in JavaScript

Variables are the first chapter of any programming story. Every program needs a way to store information so it can be used later.
This information might be a person's name, age, login status, or even the result of a calculation.
What Are Variables?
A simple way to understand variables is to think of them as boxes that store information.
Imagine you have a box.
You can put something inside it, label the box, and use it later or even somebody else can use it properly depending one how accurate the label is.
That’s exactly how variables work.
let name = "Rahul";
Here:
name → label we put on the box
"Rahul" → what we put inside the box
Now whenever we use name, JavaScript knows that we are referring to "Rahul".
Variables help programs remember data while the program runs.
But Why Do We Need Variables?
Programs constantly work with data.
For example, a website might need to store:
a user's name
their age
whether they are logged in
items in their shopping cart
Without variables, a program would not be able to store or reuse any of this information.
But before going any further in variables, let us understand an important concept:
Understanding Scope
Scope determines where a variable can be accessed in a program.
A simple way to imagine scope is to think of it as rooms inside a house.
If you place something inside one room, you can access it inside that room but not necessarily outside it.
Example:
//pseudo-code
{
variable message = "Hello";
print(message); // works
}
print(message); // Error
Here the variable exists only inside the block { }. This means the variable is block scoped.
This helps keep variables organized and prevents conflicts between different parts of the program.
Declaring Variables in JavaScript
In JavaScript, variables can be declared using three keywords: var , let and const.
Each one creates a variable, but they behave slightly differently.
Using var:
var was the original way of declaring variables in JavaScript.
Example:
var city = "Delhi";
console.log(city);
The value of a var variable can also be changed later.
var city = "Delhi";
city = "Mumbai";
However, var had a huge downside, it does not respect block scope, In other words if you label a box using var outside a function's scope, it attaches itself to the global scope and then if you use the same label in a different file of the global scope then, the value of the previous box will also change.
For example:
if (true) {
var x = 10;
}
console.log(x); // 10
Even though x was declared inside an if block, it is still accessible outside because var ignores block scope.
Now imagine using var i in a for loop , if somebody else uses var i in a different for loop then it will affect your for loop code leading to whole next level of chaos.
Using let:
let is a more modern way to declare variables.
Example:
let score = 50;
The value of a let variable can be changed later.
let score = 50;
score = 70;
But let does not allow redeclaring the same variable in the same scope.
Unlike var, it respects the block scope, so a variable declared using let inside a scope, remains in that scope and is unaccessible outside.
if (true) {
let x = 10;
}
console.log(x);
Output:
ReferenceError: x is not defined
So a for loop iterator defined using let will never interfere with a different for loop.
Using const:
In bigger programs, it is preferred to keep value of a variable constant as much as possible. const is used when the value of variable should not change.
Example:
const birthYear = 2000;
Trying to change it will cause an error.
const birthYear = 2000;
birthYear = 2001; // Error
Because of this, many developers follow this rule:
Use const by default
Use let if the value needs to change
Similar to let , it respects the block scope.
In modern programming, var is never used. let and const are always preferred.
Comparison: var vs let vs const
| Feature | var | let | const |
|---|---|---|---|
| Can change value | Yes | Yes | No |
| Can redeclare | Yes | No | No |
| Modern usage | Super rare | Common | Very common |
Primitive Data Types in JavaScript
Variables store different kinds of data.
The type of data stored in a variable is called a data type.
JavaScript has several primitive data types.
The most common ones are:
String
Number
Boolean
Null
Undefined
1. String
Strings store text values.
Example:
let name = "Aman";
let message = "Hello World";
Strings are written inside quotes.
They are used for storing:
names
emails
messages
addresses
2. Number
Numbers store numeric values.
Example:
let age = 25;
let price = 199.99;
JavaScript uses the same data type for both integers and decimals.
Numbers are used for:
calculations
prices
scores
counts
3. Boolean
Booleans represent true or false values.
Example:
let isLoggedIn = true;
let hasAccess = false;
Booleans are commonly used in conditions and decision making.
Example questions a program might ask:
Is the user logged in?
Is the password correct?
4. Null
null represents an intentional empty value.
Example:
let selectedUser = null;
This means the variable exists, but we haven’t assigned a real value yet.
5. Undefined
If we declare a variable but do not assign a value, JavaScript automatically assigns it undefined.
Example:
let score;
console.log(score); //undefined
Here the variable exists, but it does not contain any value yet.
Difference Between null and undefined
| Value | Meaning |
|---|---|
| undefined | JavaScript automatically assigns it |
| null | Assigned intentionally by the programmer |
Conclusion
Variables are the beginning of programming because they allow programs to store and reuse information.
In JavaScript, variables can be created using var, let, and const, each with slightly different behavior.
We also learned that variables can store different kinds of data, such as strings, numbers, Boolean, null, and undefined.
Understanding variables and data types is an important first step toward writing meaningful programs in JavaScript.




