Back to articles
Map and Set in JavaScript

Map and Set in JavaScript

via Dev.toSouvik Guha Roy

JavaScript offers Map and Set as modern data structures to solve common problems with traditional objects and arrays. Let’s explore what they are, how they differ, and when to use them. 🧠 What Is a Map? A Map is a collection of key-value pairs , similar to an object, but with some improvements: Keys can be any type (objects, functions, primitives) Preserves insertion order Built-in methods to get, set, delete, and check entries ✅ Map Example ```js id="map1" const map = new Map(); map.set("name", "Rahul"); map.set("age", 22); map.set(true, "Boolean key"); console.log(map.get("name")); // Rahul console.log(map.has("age")); // true console.log(map.size); // 3 --- ### 📊 Map Visual ```id="viz1" Key → Value "name" → "Rahul" "age" → 22 true → "Boolean key" ⚡ Map vs Object Feature Map Object Key types Any type String / Symbol Order preservation Yes Not guaranteed Size property map.size Must compute manually Iteration Built-in Manual / Object.keys 🧠 What Is a Set? A Set is a collection of uniqu

Continue reading on Dev.to

Opens in a new tab

Read Full Article
3 views

Related Articles