Understanding Variables and Data Types in JavaScript
When learning JavaScript, one of the first things you need to understand is variables and data types.
They are used to store and manage information in a program.
For example, in an application you might store:
A user’s name
A person’s age
Whether someone is a student or not
To store this information, JavaScript uses variables.
What Are Variables?
A variable is like a box that stores information.
Think of it like labeling a box:
Box labeled name → stores a person's name
Box labeled age → stores a number
Box labeled isStudent → stores true or false
Example in JavaScript:
let name = "Ram";
let age = 22;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output:
Ram
22
true
Here, name, age, and isStudent are variables storing different values.
Declaring Variables in JavaScript
JavaScript provides three ways to declare variables:
varletconst
Example:
var city = "Pune";
let age = 25;
const country = "India";
Each of these works slightly differently.
var
var was used in older JavaScript versions.
Example:
var language = "JavaScript";
console.log(language);
The problem with var is that it can sometimes behave unexpectedly because of its function scope.
Because of this, developers now prefer let and const.
let
let is used when the value may change later.
Example:
let score = 50;
score = 70;
console.log(score);
Output
70
The value changed from 50 → 70.
const
const is used when the value should not change.
Example:
const pi = 3.14;
console.log(pi);
If we try to change it:
pi = 3.14159;
JavaScript will give an error, because const values cannot be reassigned.
Primitive Data Types in JavaScript
Data types describe the type of data stored in a variable.
JavaScript has several primitive data types.
String
A string represents text.
Example:
let name = "Ram";
console.log(name);
Examples of strings:
"Hello"
"JavaScript"
"Web Developer"
Number
Numbers represent numeric values.
Example:
let age = 25;
let price = 100;
console.log(age);
console.log(price);
Numbers can include:
integers → 10
decimals → 3.14
Boolean
A boolean can have only two values:
truefalse
Example:
let isLoggedIn = true;
console.log(isLoggedIn);
This is commonly used for conditions.
null
null means intentionally empty value.
Example:
let selectedUser = null;
This means the variable currently has no value assigned intentionally.
undefined
undefined means a variable has been declared but not assigned a value yet.
Example:
let score;
console.log(score);
Output:
undefined
Basic Difference Between var, let, and const
| Keyword | Can Change Value | Scope |
|---|---|---|
| var | Yes | Function scope |
| let | Yes | Block scope |
| const | No | Block scope |
Most modern JavaScript uses:
let → when value can change
const → when value should stay the same
What is Scope?
Scope means where a variable can be used in the code.
Example:
if (true) {
let message = "Hello";
console.log(message);
}
This works inside the block.
But outside:
console.log(message);
This will cause an error, because the variable exists only inside that block.
Think of scope like a room.
Variables created inside a room can only be used inside that room.
Example
Let's declare variables for name, age, and student status.
let name = "Ram";
let age = 21;
let isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Changing let vs const
Using let:
let age = 20;
age = 22;
Works fine.
Using const:
const country = "India";
country = "USA";
This causes an error because const values cannot change.
Conclusion
Variables are used to store data in JavaScript programs.
Understanding variables and data types is one of the most important basics when learning JavaScript.
Key things to remember:
Variables store information
JavaScript uses
var,let, andconstPrimitive data types include string, number, boolean, null, and undefined
letallows value changesconstkeeps values constant
Learning these basics will help you build strong JavaScript fundamentals.