Back to JavaScript Notes
Topic #333

Map Methods

You will be able to create a JavaScript Map, store and retrieve values with set and get, and iterate over its entries in insertion order.

What it is

A Map is a built-in JavaScript collection that stores key-value pairs. Unlike a plain object, a Map can use any value as a key, including objects, functions, and primitives, and it remembers the order in which entries were inserted. Related terms include set, get, has, delete, size, and iteration methods such as forEach, keys, values, and entries.

Why it matters

  • It gives predictable insertion-order iteration.
  • It avoids accidental prototype-chain lookups that can happen with plain objects.
  • It supports non-string keys, such as DOM nodes or objects.
  • It provides a clear API for checking, updating, and removing entries.

Syntax or steps

The smallest useful pattern is to create a Map, add entries with set, read entries with get, and loop through pairs with for...of.

const m = new Map();
m.set("a", 1);
m.set("b", 2);
m.get("a");
m.has("b");
m.delete("a");
m.size;

Example

const scores = new Map();

scores.set("Ada", 95);
scores.set("Grace", 98);
scores.set("Alan", 92);

console.log(scores.get("Grace"));
console.log(scores.has("Ada"));

for (const [name, score] of scores) {
  console.log(name, score);
}

scores.forEach((score, name) => {
  console.log(name, score);
});

First, new Map() creates an empty collection. set adds or updates a key-value pair. get returns the value for a key, or undefined if the key is missing. has checks whether a key exists. The for...of loop destructures each entry into name and score. The forEach callback receives value first, then key.

Common mistakes

  • Using scores["Ada"] instead of scores.get("Ada"). Map entries are not normal object properties.
  • Assuming get throws when a key is missing. It returns undefined, so use has if you need to distinguish missing keys from stored undefined.
  • Using for...in to iterate a Map. Use for...of, forEach, or entries.
  • Expecting object keys to be stringified. With a Map, {} and "{}" are different keys.

When to use it

Use a Map when you need ordered key-value storage, non-string keys, or a collection that should not inherit object properties. Use a plain object when the data is a simple record with string or symbol keys and you want literal syntax.

NeedUse
Insertion-order iterationMap
Object or function keysMap
Simple JSON-like recordPlain object
Weak references to objectsWeakMap

Practice

  1. Guided: Create a Map called prices, set three items, get one price, and log every item with for...of.
  2. Challenge: Add a method or loop that increases every price by 10, then print the updated Map. Hint: use forEach or entries and set.

Quick check

What does m.get("missing") return if "missing" was never set?

It returns undefined.

Summary

A JavaScript Map is an ordered key-value collection with explicit methods for getting, setting, checking, and iterating entries. Choose it over a plain object when key type, order, or clean iteration matter.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Map Methods – FAQs

Quick answers about learning Map Methods in JavaScript.

This free note from Coding Now Tech Institute explains Map Methods in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Map Methods, is 100% free with no signup required.
With focused practice, most students grasp Map Methods in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now