Data Types Explained - Primitive vs Reference
We are learning data types, which are closely related to variables.
A variable works like a container, and the data type tells us what kind of value is stored inside that container.
For example:
- A container can hold water, sugar, or gas
- Similarly, a variable can store text, numbers, or true/false values
What are Data Types?
Data types define the type of value stored inside a variable.
Examples:
"Anil"→ String29→ Numbertrue→ Boolean
Types of Data Types
JavaScript has two main categories:
1. Primitive Data Types (Simple Values)
- String
- Number
- Boolean
- Undefined
- Null
- Symbol
- BigInt
2. Reference Data Types (Complex Values)
- Object
- Array
- Function
- Date
- Map / Set
Primitive Data Types Explained
1. String
Used to store text. Always written inside quotes.
2. Number
Used to store numeric values.
3. Boolean
Used for true/false values.
4. Undefined
Variable is declared but no value assigned.
5. Null
Represents intentionally empty value.
6. BigInt
Used for very large numbers.
7. Symbol
Used for unique values (advanced concept).
Reference Data Types
These store multiple or complex values.
Object Example
typeof Operator
To check the data type of a variable:
Code Example
Explanation of Code
Case 1
Output:
Reason:
- Value is inside quotes
- So it is treated as a string
Case 2 (Commented in your code)
If used:
Output:
Reason:
- No quotes
- So it is a boolean value
Dynamic Typing in JavaScript
JavaScript allows changing the type of a variable: