JavaScript Intermediate
What are the differences between Map and Object? Why use Map when Object exists?
Map vs Object
Object
- Keys can only be strings or Symbols
- Inherits default prototype properties
- No built-in
sizeproperty - Integer keys sorted before string keys
const obj = {};
obj[1] = 'one';
Object.keys(obj); // ['1', ...] (numeric keys first)
Map
- Keys can be any type
- No prototype inheritance
- Built-in
sizeproperty - Strictly preserves insertion order
const map = new Map();
map.set('name', 'Alice');
map.set(42, 'answer');
map.size; // 2
map.get(42); // 'answer'
When to Use Map vs Object
| Scenario | Use |
|---|---|
| Non-string keys | Map |
| Frequent add/delete | Map |
| Insertion order matters | Map |
| Need .size | Map |
| JSON serialization | Object |
| Static config | Object |
✦ AI Mock Interview
Type your answer and get instant AI feedback
Sign in to use AI scoring
