Skip to content
Justice Tech School

Maps

A Map stores key and value pairs like an object, but keys can be any value and the insertion order is kept. Use set, get, has, delete and size, and loop with for...of.

Example
const stock = new Map();
stock.set("apple", 5);
stock.set("mango", 12);
console.log(stock.get("mango"));
console.log(stock.has("kiwi"));
for (const [fruit, qty] of stock) {
  console.log(fruit, qty);
}
console.log(stock.size);

Exercise

Read a value from the map.

stock.("apple")

Quiz Test

Can a Map use an object as a key?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.