Map and Set
ECMAScript 2026
Use a Map for key–value entries and a Set for unique values.
On this page
Look up a value by key with Map
Map pairs keys with values and keeps insertion order. set(key, value) adds or replaces an entry. get(key) reads it, and has(key) tells you whether the key exists.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const prices = new Map();
prices.set("Pen", 1500);
prices.set("Notebook", 2500);
console.log(prices.get("Pen"));
console.log(prices.has("Folder"));
What to expect Pen’s price is 1500 and Folder is absent.
Keep unique values with Set
A Set keeps each value once. For strings this compares the exact text. Use add(value) to insert, has(value) to check membership, and size to count unique values.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const products = new Set(["Pen", "Notebook", "Pen"]);
console.log(products.size);
console.log([...products]);
What to expect The size is 2. The values are Pen and Notebook.