QA
Beginner LevelJavaScript & TypeScript

What are JavaScript Set and Map objects and how do they differ from Arrays and Objects?

❓ Interview Prompt: Compare Set vs Array (unique values) and Map vs Object (any key type, size property, performance).

ES6 introduced Set and Map collections designed for specific data manipulation needs:

1. Set (Unique Value Collection):

•
Stores unique values of any type (duplicates automatically ignored).
•
Fast element lookup via .has(value) in O(1) time vs Array .includes() in O(N) time.

2. Map (Key-Value Dictionary):

•
Keys can be any type (objects, functions, primitives) unlike Plain Objects (where keys are coerced to strings).
•
Remembers insertion order during iteration.
•
Built-in .size property (no need for Object.keys().length).

Code Implementation Example

javascript
// Set: Deduplicating an array in one line
const numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)]; // [1, 2, 3, 4, 5]

// Map: Object keys in Map
const userMap = new Map();
const keyObj = { id: 100 };

userMap.set(keyObj, 'Admin User');
userMap.set('role', 'Superadmin');

console.log(userMap.get(keyObj)); // "Admin User" (Object is valid key!)
console.log(userMap.size);       // 2
💡Array deduplication with Set and object keys stored inside Map.
🎯 Interview Key Takeaway

Use Set for unique collections and fast lookups. Use Map when keys are non-strings or entry counts must be tracked.

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?