Understanding Variables and Data Types in JavaScript for Beginners

When learning JavaScript, one of the first concepts you encounter is variables.
Variables help us store and manage data in our programs.
Think of a variable like a box that stores information.
For example:
A box labeled name can store
"John"A box labeled age can store
21A box labeled isStudent can store
true
In programming, we use variables to store these values.
What Are Variables?
A variable is a container used to store data that can be used later in a program.
Example:
let name = "John";
let age = 21;
Here, name stores "John", and age stores 21
We can then use these values in our program.
Example:
console.log(name); // John
console.log(age); // 21
Declaring Variables in JavaScript
In JavaScript, variables can be declared using three keywords var, let, and const
Example:
var city = "Delhi";
let age = 20;
const country = "India";
Each of these works slightly differently.
Primitive Data Types in JavaScript
Data stored in variables has a type.
The most common primitive data types are:
1) String
Stores text.
let name = "Pritesh";
Example: "Hello", "JavaScript"
2) Number
Stores numeric values.
let age = 25;
Example: 10, 3.14, 100
3) Boolean
Stores true or false values.
let isStudent = true;
Example: true, false
4) Null
Represents intentional empty value.
let data = null;
5) Undefined
A variable declared but not assigned a value.
let score;
Value becomes undefined.
Difference Between var, let, and const
JavaScript introduced let and const in ES6 to improve how variables behave.
The diagram shows their differences clearly.
Explanation:
varis older and has function scopeletis modern and has block scopeconstis used when the value should not change
Example:
let age = 20;
age = 25; // allowed
But:
const country = "India";
country = "USA"; // error
Basically, const cannot be changed once assigned.
What Is Scope?
Scope defines where a variable can be accessed in the program. See below diagram clearly to understand scope.
Think of scope like areas inside a building.
Global scope → accessible everywhere
Function scope → accessible only inside function
Block scope → accessible only inside
{}block
Your diagram visualizes this idea clearly.
Scope Visualization Explanation
The diagram shows three layers:
Global Scope
Accessible anywhere in the program.Function Scope
Accessible only inside a function.Block Scope
Accessible only inside{}such asif,for.
Example:
let a = 10;
if(true){
let b = 20;
}
console.log(a); // works
console.log(b); // error
Because b exists only inside the block.
Example Program Using Variables
let name = "Rahul";
let age = 21;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output:
Rahul
21
true
Practice Assignment
Try this in your JavaScript console.
- Declare variables
let name = "Alice";
let age = 22;
let isStudent = true;
- Print them
console.log(name); //Alice
console.log(age); // 22
console.log(isStudent); //true
- Change values for
let
Works because let allows reassignment.
age = 23;
console.log(age); //23
- Try changing
const
const country = "India";
country = "USA";
This will produce an error.
Conclusion
Variables are the foundation of programming.
In JavaScript we, Use let for values that may change, Use const for values that should stay constant and avoid using var in modern code
Understanding variables, data types, and scope helps you write clean and predictable JavaScript programs.




