Understanding Variables and Data Types in JavaScript
When we write programs, we need a place to store information.
For example:
Your name
Your age
Whether you are a student
In JavaScript, we store this information using variables.
Let’s understand this step by step in the simplest way possible.
1. What Are Variables?
Think of a variable like a box.
This box can store information.
Example in real life:
Box → contains toys
Bottle → contains water
Wallet → contains money
In programming:
Variable → contains data
Example:
let name = "Rahul";
Here:
name→ the box"Rahul"→ the data inside the box
We can now use this variable anywhere in the program.
console.log(name);
Output:
Rahul
2. Why Do We Need Variables?
Variables help us store and reuse data.
Example:
let age = 25;
console.log(age);
console.log(age + 5);
Output:
25
30
Without variables, we would have to write numbers or values again and again.
Variables make programs clean and easier to manage.
3. Declaring Variables in JavaScript
In JavaScript, we can create variables using:
varletconst
Let’s look at each.
4. Using var
var is the older way of declaring variables.
Example:
var city = "Delhi";
console.log(city);
Output:
Delhi
You can also change the value later.
var city = "Delhi";
city = "Mumbai";
console.log(city);
Output:
Mumbai
However, modern JavaScript usually prefers let or const.
5. Using let
let is the modern way to declare variables.
Example:
let age = 20;
console.log(age);
You can change the value later.
let age = 20;
age = 21;
console.log(age);
Output:
21
Use let when the value may change.
6. Using const
const means constant.
The value cannot be changed.
Example:
const country = "India";
console.log(country);
If you try to change it:
const country = "India";
country = "USA";
JavaScript will show an error.
So use const when the value should stay the same.
Simple Comparison
| Keyword | Can Change Value? | Modern Usage |
|---|---|---|
| var | Yes | Rarely used |
| let | Yes | Common |
| const | No | Very common |
7. What Are Data Types?
Variables store different kinds of data.
These kinds are called data types.
Example:
Name → text
Age → number
IsStudent → true/false
JavaScript has many types, but beginners should know these primitive data types.
8. Primitive Data Types in JavaScript
1. String
A string is text.
Example:
let name = "Ravi";
Other examples:
"Hello"
"JavaScript"
"My Name"
Strings are written inside quotes.
2. Number
Numbers represent numeric values.
Example:
let age = 25;
Examples:
10
100
3.14
Numbers can be used for math calculations.
3. Boolean
Boolean means true or false.
Example:
let isStudent = true;
Possible values:
true
false
This is useful for conditions and decisions.
Example:
IsLoggedIn → true
IsLoggedIn → false
4. Null
null means empty value.
Example:
let data = null;
It means the variable exists but has no value.
5. Undefined
undefined means a variable is declared but no value is assigned.
Example:
let score;
console.log(score);
Output:
undefined
Because we created the variable but did not give it a value.
9. What is Scope? (Simple Explanation)
Scope means:
Where a variable can be used in the code
Example:
{
let number = 10;
console.log(number);
}
Inside the block → works.
But outside the block:
console.log(number);
It will show an error.
So scope means the area where a variable is accessible.
Simple idea:
Inside the box → variable works
Outside the box → variable may not work
Assignment Practice
Try this in your browser console.
1. Declare Variables
let name = "Arjun";
let age = 22;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output:
Arjun
22
true
2. Change Value of let
let age = 22;
age = 23;
console.log(age);
Output:
23
Works because let allows changes.
3. Try Changing const
const country = "India";
country = "USA";
Result:
Error
Because const cannot be changed.
Simple Diagram
Variable
name
↓
"Rahul"
Data Types
String → "Hello"
Number → 25
Boolean → true
Null → empty value
Undefined → value not assigned