Understanding Variables and Data Types in JavaScript
When learning JavaScript, one of the first things you encounter is variables and data types.
Variables allow us to store and manage information inside a program.
For example:
A user’s name
A person’s age
Whether a student is enrolled or not
Understanding these concepts is essential because almost every program relies on variables.
What Are Variables?
A variable is like a box used to store information.
Imagine you have labeled boxes:
| Box Label | Stored Value |
|---|---|
| name | Viral |
| age | 22 |
| city | Ahmedabad |
In programming, we store these values using variables.
Example:
let name = "Viral";
let age = 22;
Here:
namestores"Viral"agestores22
The program can use or modify these values later.
Declaring Variables in JavaScript
In JavaScript, variables can be declared using:
varletconst
Example:
var city = "Ahmedabad";
let age = 22;
const country = "India";
Each keyword behaves slightly differently.
var
var is the older way to declare variables.
Example:
var language = "JavaScript";
console.log(language);
Output:
JavaScript
However, modern JavaScript developers prefer let and const.
let
let is used when the value may change later.
Example:
let score = 10;
score = 20;
console.log(score);
Output:
20
Here the value changed from 10 → 20.
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.1415;
JavaScript will produce an error.
So const is used for fixed values.
Primitive Data Types in JavaScript
Data types define what type of value a variable holds.
Here are the most common primitive data types.
String
Strings represent text values.
Example:
let name = "Viral Jain";
Examples of strings:
Names
Cities
Messages
Number
Numbers represent numeric values.
Example:
let age = 22;
let price = 199.99;
Numbers can represent:
age
marks
price
quantity
Boolean
Booleans represent true or false values.
Example:
let isStudent = true;
Boolean values are often used in conditions.
null
null means intentional empty value.
Example:
let selectedUser = null;
It means the variable currently has no value assigned intentionally.
undefined
undefined means a variable exists but has not been assigned a value yet.
Example:
let phoneNumber;
console.log(phoneNumber);
Output:
undefined
Difference Between var, let, and const
| Feature | var | let | const |
|---|---|---|---|
| Modern JavaScript | No | Yes | Yes |
| Can change value | Yes | Yes | No |
| Recommended | No | Yes | Yes |
| Used for constants | No | No | Yes |
In modern JavaScript:
Use let for variables that change
Use const for fixed values
What is Scope? (Beginner Explanation)
Scope means where a variable can be accessed in your code.
Example:
{
let message = "Hello World";
console.log(message);
}
Inside the block { }, the variable is accessible.
But outside:
console.log(message);
This would cause an error because the variable exists only inside that block.
So scope defines where variables live and can be used.
Assignment Practice
Try the following example in your browser console.
Step 1: Declare Variables
let name = "Viral";
let age = 22;
let isStudent = true;
Step 2: Print Them
console.log(name);
console.log(age);
console.log(isStudent);
Output:
Viral
22
true
Experiment With let and const
Changing a let value
let age = 22;
age = 23;
console.log(age);
Output:
23
Changing a const value
const country = "India";
country = "USA";
This will produce an error because const values cannot change.