**Set Object
Set
A Set stores unique values only (no duplicates) and maintains insertion order.
Syntax: const st = new Set();
Set Use Cases
-
Use when you need unique values (e.g., unique IDs, tags, categories).
-
Great for removing duplicates from arrays.
-
Fast search, add, delete operations compared to arrays.
Set Methods
.add()
Adds a value to the Set.
Syntax: st.add(value)
.has()
Checks whether a value exists.
Syntax: st.has(value)
.size
Returns number of unique items.
Syntax: st.size
.delete()
Removes a specific value.
Syntax: st.delete(value)
Interesting Fact Why Objects Behave Uniquely in Set
1. Objects are stored by reference, not by value
When you put objects inside a Set, JavaScript checks memory address, not values.
2. Your first object
let employee1 = { id: 1, name: "Arun" };
This object has its own memory address.
Example: 0x001 (just for explanation)
3. The array of employees
const employeeDetails = [ employee1, { id: 2, name: "Prasanna" } ];
Then you convert it into a Set:
const employeeDetailsSet = new Set(employeeDetails);
So the Set contains:
-
Object at address
0x001→{ id: 1, name: "Arun" } -
Object at another address →
{ id: 2, name: "Prasanna" }
❌ Delete attempt 1
employeeDetailsSet.delete({ id: 1, name: "Arun" });
This looks the same but is a new object in memory, like:
{ id: 1, name: "Arun" } → address 0x999
Since 0x999 ≠ 0x001, Set will NOT delete it.
✅ Delete attempt 2
employeeDetailsSet.delete(employee1);
Here you pass the exact same object reference (employee1), same memory address 0x001.
So this time deletion works ✔️
🎯 Final Output Explanation
Before delete:
Set contains 2 objects.
After delete attempt 1:
Nothing removed (different reference).
After delete attempt 2:
employee1 is removed.
🧠 Key Takeaway for Interviews
In JavaScript, Sets treat objects based on reference, not value.
Two objects with identical values are NOT equal unless they refer to the same memory address.
.clear()
Removes all values.
Syntax: st.clear()
return value is undefined
.entries()
Returns [value, value] pairs (for compatibility with Map).
Syntax: st.entries()
.forEach()
Loops through values.
Syntax: st.forEach(val => { ... })
**WeakSet
A WeakSet stores only objects and allows automatic garbage collection.
Syntax: const ws = new WeakSet();
**WeakSet Use Cases
-
Track objects without preventing garbage collection
-
Store temporary state, e.g., "which objects are processed?"
-
Ideal for private object tracking in classes or modules
Comparison: Map vs Set vs WeakMap vs WeakSet
| Feature | Map | Set | WeakMap | WeakSet |
|---|---|---|---|---|
| Stores | Key–value pairs | Unique values | Key–value pairs | Unique object values |
| Key Type | Any type (string, number, object) | Any value | Objects only | Objects only |
| Value Type | Any | Any | Any | Objects only |
| Duplicates Allowed? | No duplicate keys | No duplicate values | No duplicate keys | No duplicate values |
| Garbage Collection | ❌ No | ❌ No | ✅ Yes (auto removes unused object keys) | ✅ Yes (auto removes unused objects) |
| Iterable? | ✅ Yes | ✅ Yes | ❌ No | ❌ No |
| Has size property? | ✅ map.size |
✅ set.size |
❌ No | ❌ No |
| Methods | set, get, has, delete, clear, entries, forEach | add, has, delete, clear, entries, forEach | set, get, has, delete | add, has, delete |
| Order preserved? | Yes | Yes | No (not iterable) | No (not iterable) |
| Primary Use Case | Fast lookup table | Unique collection of values | Private data for objects | Track objects without memory leaks |
1-Line Interview Descriptions
**Map
“Ordered key–value store where keys can be any type; good for fast lookups.”
**Set
“Stores unique values with fast membership checks; prevents duplicates.”
**WeakMap
“Map that stores only object keys with automatic garbage collection; not iterable.”
**WeakSet
“Set that stores only objects; auto garbage-collection; useful for tracking objects.”
Reference Code
Time & Space Complexity: Learn More
Source Link : https://github.com/er-arunkumarselvam/ES6--JavaScript/blob/main/assets/js/setObject.js