Notes – JavaScript Data Types
In JavaScript, data types define the kind of values that can be stored and manipulated in a program. JavaScript is a dynamically typed language, meaning you donโt need to declare variable types explicitly.
Categories of Data Types
JavaScript has two main categories of data types:
- Primitive Data Types (Immutable, stored directly in memory)
- Non-Primitive (Reference) Data Types (Stored by reference)
1. Primitive Data Types
These are the most basic data types in JavaScript. They hold a single value and are immutable.
| Data Type | Description | Example |
|---|---|---|
| Number | Represents integers & floating-point numbers | let age = 25; |
| String | Represents text (sequence of characters) | let name = "John"; |
| Boolean | Represents true/false values | let isStudent = false; |
| Undefined | A variable declared but not assigned any value | let x; |
| Null | Represents an intentional empty value | let y = null; |
| Symbol (ES6) | Unique and immutable identifier | let sym = Symbol("id"); |
| BigInt (ES11) | Used for very large numbers beyond Number limits | let big = 12345678901234567890n; |
Example of Primitive Data Types
let num = 10; // Number
let text = "Hello"; // String
let isValid = true; // Boolean
let value; // Undefined
let empty = null; // Null
let uniqueID = Symbol('id'); // Symbol
let bigNum = 12345678901234567890n; // BigInt
2. Non-Primitive (Reference) Data Types
These data types store complex structures and are mutable.
| Data Type | Description | Example |
|---|---|---|
| Object | Collection of key-value pairs | let person = { name: "John", age: 30 }; |
| Array | Ordered list of values | let numbers = [10, 20, 30]; |
| Function | A reusable block of code | function greet() { console.log("Hi"); } |
Example of Non-Primitive Data Types
let user = { name: "Alice", age: 25 }; // Object
let colors = ["red", "green", "blue"]; // Array
function sayHello() { console.log("Hello!"); } // Function
Checking Data Types
You can check the data type of a variable using typeof.
console.log(typeof 10); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (JavaScript quirk!)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
Note: typeof null returns "object" due to a JavaScript bug that has existed since its early days.
Difference Between Primitive & Non-Primitive Data Types
| Feature | Primitive Data Types | Non-Primitive Data Types |
|---|---|---|
| Storage | Stored directly in memory | Stored as a reference |
| Mutability | Immutable (cannot be changed) | Mutable (values can be modified) |
| Copy Behavior | Creates a copy of the value | Creates a reference (changes affect the original) |
Example: Primitive vs. Non-Primitive Copy Behavior
Primitive Copy
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (original remains unchanged)
console.log(b); // 20 (new copy modified)
Non-Primitive Copy
let obj1 = { name: "Alice" };
let obj2 = obj1;
obj2.name = "Bob";
console.log(obj1.name); // "Bob" (Both references point to the same object)
