JavaScript Variables Explained (var, let, const, Data Types & Scope)
Every program needs a way to store information.
For example:
Your name
Your age
Whether you are a student
Your account balance
Programming languages store this information using variables.
What is a Variable?
Think of a variable like a box that stores information.
Example in real life:
Box labeled Name → contains "Gagan"
Box labeled Age → contains 22
In JavaScript, we store information in variables like this:
let name = "Gagan";
let age = 22;
Here:
nameis a variable storing textageis a variable storing a number
Why Do We Need Variables?
Variables help us:
store data
reuse information
update values
make programs dynamic
Example:
let score = 10;
score = score + 5;
console.log(score);
Output:
15
The program updated the value stored in the variable.
Declaring Variables in JavaScript
JavaScript provides three ways to declare variables:
varletconst
1) Using var
var was the original way to create variables in JavaScript.
Example:
var city = "Delhi";
console.log(city);
However, modern JavaScript developers rarely use var because it has confusing behavior.
2) Using let
let is the most common way to create variables today.
Example:
let age = 25;
console.log(age);
The value of a let variable can be changed.
Example:
let score = 50;
score = 80;
console.log(score);
Output:
80
3) Using const
const is used for values that should not change.
Example:
const country = "India";
console.log(country);
Trying to change it will cause an error.
const country = "India";
country = "USA"; // Error
Primitive Data Types
A data type tells JavaScript what kind of value a variable stores.
1) String
Strings represent text.
Example:
let name = "Gagan";
Examples of strings:
"Hello"
"JavaScript"
"My name is Gagan"
2) Number
Numbers represent numeric values.
Example:
let age = 22;
Examples:
10
50
3.14
1000
3) Boolean
Boolean values represent true or false.
Example:
let isStudent = true;
Possible values:
true
false
Example use case:
let isLoggedIn = false;
4) Null
null represents an empty value.
Example:
let selectedUser = null;
It means no value is assigned yet.
5️⃣ Undefined
A variable that has been declared but not assigned a value is undefined.
Example:
let score;
console.log(score);
Output:
undefined
Difference Between var, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Can redeclare | Yes | No | No |
| Can change value | Yes | Yes | No |
| Modern usage | Rare | Common | Very common |
What is Scope? (Simple Explanation)
Scope defines where a variable can be used in the program.
Think of scope like rooms in a house.
If something is inside a room, it may not be accessible outside the room.
Example:
{
let message = "Hello";
}
console.log(message);
This will produce an error because the variable only exists inside the block.
Example with correct scope:
let message = "Hello";
console.log(message);
Here the variable is accessible.
Simple Scope Visualization
4
Practical Example
let name = "Gagan";
let age = 22;
const country = "India";
console.log(name);
console.log(age);
console.log(country);
Output:
Gagan
22
India
Practice Assignment
1) Declare Variables
Create variables for:
Name
Age
IsStudent
Example:
let name = "Rahul";
let age = 20;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
2) Try Changing Values
Change the value of a let variable.
Example:
let age = 20;
age = 25;
console.log(age);
Now try the same with const.
Example:
const country = "India";
country = "USA"; // Observe the error
Comments
Join the discussionNo comments yet. Be the first to comment.