BeginnerCoder

Lesson 15 — Map & Set

Better collections

A Set stores values with no duplicates — adding the same value twice keeps one. A Map stores key/value pairs like an object, but keys can be any type and it has handy methods: set, get, has and size.

const tags = new Set(["js", "css", "js"]);
console.log(tags.size);      // 2
const price = new Map();
price.set("tea", 3);
console.log(price.get("tea")); // 3

Why it matters: removing duplicates and looking things up by key are everyday jobs, and these two do them fast and clearly.

Want it another way?
Task

Remove duplicates from nums with a Set, and add Ann (25) to the ages Map.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.