1. What Are Variables?
Think of a variable like a box that stores information.
Imagine you have labeled boxes:
One box stores your name
Another stores your age
Another stores whether you are a student
In programming, we store information inside variables so the computer can use it later.
Example:
let name = "Rahul";
let age = 18;
Here:
name stores "Rahul"
age stores 18
So a variable is simply a container for storing data.
2. How to Declare Variables in JavaScript
In JavaScript, we can create variables using three keywords:
varletconst
Using var
var city = "Delhi";
varwas used in older JavaScript versions. It still works but is less recommended today.
Using let
let age = 20;
letis commonly used when the value may change later.
Example:
let score = 10;
score = 15;
Now scorebecomes 15.
Using const
const country = "India";
constmeans constant, which means the value cannot be changed later.
Example:
const pi = 3.14;
Trying to change it will cause an error.
3. Primitive Data Types in JavaScript
A data type describes what kind of value a variable stores.
JavaScript has several primitive data types.
1. String
A string stores text.
Example:
let name = "Aman";
Other examples:
let city = "Mumbai";
let message = "Hello World";
Strings are written inside quotes ("" or '').
2. Number
A **number **stores numeric values.
Example:
let age = 18;
let price = 99.99;
Numbers can be:
Whole numbers → 20
Decimal numbers → 10.5
3. Boolean
A boolean stores only two values:
truefalse
Example:
let isStudent = true;
let isLoggedIn = false;
This type is commonly used for conditions and decisions.
4. Null
nullrepresents an empty value.
Example:
let result = null;
It means the variable exists but currently has no value.
5. Undefined
A variable is undefined when it is declared but not given a value.
Example:
let score;
Since we didn't assign anything, the value is undefined.
4. Difference Between var, let, and const
| Keyword | Can Change Value? | Modern Usage |
|---|---|---|
| var | Yes | Mostly avoided |
| let | Yes | Recommended |
| const | No | Recommended for fixed values |
Example:
let age = 18;
age = 19; // allowed
const country = "India";
country = "USA"; // error
5. What is Scope? (Beginner Explanation)
Scope means where a variable can be used in your code.
Think of scope like rooms in a house.
If you keep a box in your bedroom, it can only be accessed in that room.
Similarly, some variables can only be used inside certain parts of the code.
Example:
{
let message = "Hello";
console.log(message);
}
Here message works inside the block, but outside it cannot be used.
This helps keep code organized and safe.
6. Practical Example
let name = "Rohan";
let age = 19;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output in console:
Rohan
19
true
Assignment Practice
Try writing the following code:
let name = "Rahul";
let age = 18;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Now experiment:
- Change the value of
name - Change the value of
age - Try creating a
constvariable and change it
Example:
const country = "India";
country = "USA"; // observe the error
Observe how JavaScript behaves.
Top comments (0)