Think of variables as box that stores information,so it you multiple box then you need to label the box to understand what is stored inside.
In programming varibale stores data and vairable name is label
let name = "John";
Here name is label and John is value
How to declare varaibles
we can use let,var & const to declare variable
let age = 25;
const country = "USA"
var isStudent=true
Primitives data type
- String
let name = "Alice";
- number
let age = 20;
- Boolean
let isStudent = true;
- null
let middleName = null;
- undefined
let score;
When to user var,let & const:
var : declares function-scoped or globally-scoped variables
let : declares a block-scoped variable
const: declares a block-scoped variable, assigning a value that cannot be re-assigned.
What is block-scopewhere
Scope means where your variable is used.
Think of variable as scope in your room:
- if you put your box in living room everyone can see it
- if you put your box in your bedroom only you can see it
{
let message = "Hello";
console.log(message); // ✅ Works here
}
console.log(message); // ❌ Error (not accessible outside)
Declare Variables
let name = "Emma";
let age = 22;
let isStudent = true;
Try Changing
age = 23; // ✅ Allowed
isStudent = false; // ✅ Allowed
Now try with const:
const country = "Canada";
country = "USA"; // ❌ What happens?
Scope Visulization
Global Scope
├── name
├── age
└── Block Scope
└── message
Top comments (0)