1. Primitive Datatypes
Primitive types are the basic building blocks of JavaScript.
They are simple, immutable values — meaning they don’t hold references or complex structures.
Each primitive directly represents a single value.
Primitive types include:
- Number
- String
- Boolean
- Undefined
- Null
- BigInt
- Symbol
💡 F*un fact: *When you copy a primitive, you’re copying the actual value, not a reference.
Example:
let a = 10;let b = a; // b gets a copy of the value 10b = 20;console.log(a); // 10 (unchanged)undefined vs null
| Concept | undefined | null |
|---|---|---|
| Meaning | Automatically assigned to a variable that’s declared but not initialized | Intentionally assigned to represent “no value” |
| Type | undefined | object*(a legacy bug in JS)* |
| Behavior | JavaScript sets it by default | Developer sets it explicitly |
| Example | let a; console.log(a) → undefined | let a = null; console.log(a) → null |
💡 I**n short:
- undefined → “I haven’t been given a value yet.”
- null → “I’ve been deliberately emptied.”
Number vs BigInt
| Feature | Number | BigInt |
|---|---|---|
| Description | 64-bit floating-point number | Arbitrary-precision integer |
| Range | ±(2⁵³ − 1) | Limited only by memory |
| Usage | Everyday calculations | Huge integer values |
| Example | let x = 42; | let big = 42n; |
⚠️ Note: You can’t mix them directly.
10n + 10; // ❌ TypeErrorSymbol
A Symbol creates a unique, immutable identifier every time it’s called — even with the same description.
Symbol('id') === Symbol('id'); // falseUsed for hidden or private object keys that don’t appear in normal enumeration.
const meta = Symbol('meta');const user = { name: 'Alice' };
user[meta] = { lastUpdated: 'today' };
console.log(Object.keys(user)); // ['name']console.log(user[meta]); // { lastUpdated: 'today' }2. Non-Primitive Datatypes
Non-primitive types are also known as reference types because they don’t store the actual value — they store a reference to where the value lives in memory.
They can hold complex structures and collections of data.
They’re more flexible and can store multiple values under one variable.
Non-primitive types include:
- Object
- Array (which is actually a special kind of object)
💡 R*emember: *When you copy or compare non-primitives, you’re dealing with references, not values.
Two objects with identical content are not equal because they live in different memory locations.
Example:
let user1 = { name: "Alice" };let user2 = user1; // both point to the same memory reference
user2.name = "Bob";console.log(user1.name); // "Bob" (because they share the same reference)In short:
| Type | Stored As | Copy Behavior | Example |
|---|---|---|---|
| Primitive | Actual value | Creates a new copy | let b = a |
| Non-Primitive | Memory reference | Shares the same reference | let obj2 = obj1 |