JavaScript Data Types: Primitive and Non-Primitive Data Types
Data types are an important concept in JavaScript because they define the kind of value a variable can store. Understanding data types helps developers write reliable and efficient code.
What is a Data Type?
A data type defines what kind of value a variable can hold.
Example
let name = "John"; // String
let age = 25; // Number
let isActive = true; // Boolean
In the above example, each variable stores a different type of value.
Types of Data Types in JavaScript
JavaScript data types are broadly classified into two categories:
- Primitive Data Types
- Non-Primitive Data Types
Primitive Data Types
Primitive data types store a single and simple value.
Characteristics
- Store a single value.
- Immutable (cannot be changed directly).
- Compared by value.
- Stored directly in memory.
Types of Primitive Data Types
1. String
Used to store textual data.
let name = "John";
2. Number
Used to store numeric values.
let age = 25;
let price = 99.99;
3. Boolean
Represents either true or false.
let isLoggedIn = true;
4. Undefined
A variable that has been declared but not assigned a value.
let city;
console.log(city); // undefined
5. Null
Represents the intentional absence of a value.
let user = null;
6. Symbol
Used to create unique identifiers.
let id = Symbol("id");
7. BigInt
Used to store very large integers beyond the safe Number limit.
let largeNumber = 123456789012345678901234567890n;
Non-Primitive Data Types
Non-primitive data types store multiple values or complex data structures.
Characteristics
- Can store collections of data.
- Mutable (their contents can be modified).
- Compared by reference.
- Stored as references in memory.
Types of Non-Primitive Data Types
1. Array
Used to store multiple values in a single variable.
let colors = ["red", "green", "blue"];
2. Object
Used to store data as key-value pairs.
let person = {
name: "John",
age: 25
};
3. Function
Functions are reusable blocks of code.
function greet() {
console.log("Hello");
}
4. Date
Used to work with dates and times.
let today = new Date();
5. Regular Expression (RegExp)
Used for pattern matching and text validation.
let pattern = /hello/i;
Primitive vs Non-Primitive Data Types
| Feature | Primitive Data Types | Non-Primitive Data Types |
|---|---|---|
| Value Type | Single and Simple Value | Multiple or Complex Values |
| Mutability | Immutable | Mutable |
| Comparison | By Value | By Reference |
| Memory Storage | Direct Value | Reference to Object |
| Examples | String, Number, Boolean | Array, Object, Function |
Example of Primitive Comparison
let a = 10;
let b = 10;
console.log(a === b); // true
The values are compared directly.
Example of Non-Primitive Comparison
let obj1 = { name: "John" };
let obj2 = { name: "John" };
console.log(obj1 === obj2); // false
Although both objects contain the same data, they occupy different memory locations.
Conclusion
JavaScript data types are divided into Primitive and Non-Primitive categories. Primitive data types store simple values and are immutable, while Non-Primitive data types store complex data structures and are mutable. Understanding the differences between these data types is essential for writing efficient and bug-free JavaScript applications.
Top comments (0)