JavaScript Map set() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

Definition

map.set(key, value) stores value under key and returns the same Map. The key can be any value: a string, a number, an object, or even NaN. If the key is already in the Map, its old value is replaced. Keys are matched with SameValueZero, so NaN equals NaN. After set(), map.size grows when the key was new.

Remember
map.set(key, value)   → the same Map

key = any value      → stored
key already there    → value is replaced
NaN as key           → works (SameValueZero)
set(a, 1).set(b, 2)  → chaining works
map.size             → grows for new keys

Syntax

Call set on a Map with any key and any value.

JavaScript
map.set(key, value)
CallWhat it does
map.set(key, value)Store value for key. Replaces an older value for the same key.
map.set(a, 1).set(b, 2)Chain calls. Each set() returns the Map, so the next call can follow it.

Built into JavaScript since ES2015. Unlike a WeakMap, a Map accepts every key type and keeps a size. To read a value back, see get().

Library declarations
JavaScript
map.set(key, value)   // returns the Map

// key:   any value (string, number, object, NaN, ...)
// value: anything
// SameValueZero: NaN matches NaN

Parameters

ParameterDescription
keyRequired. Any value: a string, number, boolean, object, function, or NaN. Matching uses SameValueZero.
valueRequired. Any value: a number, a string, an object, a function, even undefined.

Return Value

Return ValueDescription
MapThe same Map you called set() on, so calls can be chained.

JavaScript Examples

Example 1 — Store, Chain, and Replace

Store scores for two players, chain two calls together, and see what happens when the same key is stored twice.

JavaScript
const scores = new Map();

const result = scores.set("Ann", 10);
console.log(result === scores);   // true

scores.set("Bob", 8).set("Ann", 12);
console.log(scores.get("Ann"));   // 12
console.log(scores.get("Bob"));   // 8
console.log(scores.size);         // 2
Try It Yourself

How It Works

  1. set() hands back the map. scores.set("Ann", 10) returns scores itself, so the comparison with scores prints true.
  2. Calls can be chained. The next line stores Bob and then stores Ann again, because each set() gives back the Map for the call that follows.
  3. Same key, new value. Ann was stored as 10 and then as 12. A key holds one value, so the later call wins. Size stays 2 because Ann was already there.

Example 2 — String Keys and Object Keys

Store a product id as a string and a cart object as a key. See that matching shape is not enough for objects.

JavaScript
const data = new Map();
const cart = { id: 7 };

data.set("sku-100", "Mug");
data.set(cart, 2);

console.log(data.get("sku-100"));       // Mug
console.log(data.get(cart));            // 2
console.log(String(data.get({ id: 7 }))); // undefined
console.log(data.size);                 // 2
Try It Yourself

How It Works

  1. Strings work as keys. data.set("sku-100", "Mug") stores a plain string key. Later get("sku-100") finds the mug.
  2. Objects work too. The same Map stores a count under the cart object. You need that exact object to read it back.
  3. Identity, not shape. A new { id: 7 } looks the same but is a different object, so get() returns undefined. Size is still 2.

Example 3 — NaN Works as a Key

Store a tag under NaN. Every NaN matches, even though NaN === NaN is normally false.

JavaScript
const tags = new Map();

tags.set(NaN, "missing");
console.log(tags.get(NaN));           // missing
console.log(tags.get(Number("x")));   // missing
console.log(tags.size);               // 1

tags.set(NaN, "updated");
console.log(tags.get(NaN));           // updated
console.log(tags.size);               // 1
Try It Yourself

How It Works

  1. NaN can be a key. tags.set(NaN, "missing") stores one entry. Reading with get(NaN) finds it.
  2. SameValueZero matches NaN. Number("x") is also NaN, so it finds the same entry. Size stays 1.
  3. Replace, do not grow. Setting NaN again replaces the value with "updated". Size is still 1.

Browser & Runtime Support

Map.prototype.set() is part of ES2015 (ES6), together with WeakMap, Set, and WeakSet.

✓ Baseline · ES2015

Map.prototype.set()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic Map with set(), get(), has(), delete(), clear(), and size.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
Map.prototype.set() Excellent

Bottom line: Safe to use in any modern browser and runtime. set() has behaved the same way since Map first shipped.

Frequently Asked Questions

It stores a value under a key in a Map and returns the Map. The key can be any value. If the key already exists, its value is replaced.
Yes. set() returns the Map, so map.set(a, 1).set(b, 2) works. This is a handy way to fill a Map in one statement.
Yes. A key has only one value. Calling set() again with the same key overwrites the old value. size does not grow when you replace.
Any value: strings, numbers, objects, functions, booleans, null, undefined, and NaN. Object keys are matched by identity. NaN matches NaN.
A Map accepts any key and keeps a size. A WeakMap only accepts object keys (and local symbols), has no size, and lets dead keys be cleaned up.

Next: get()

Read values back from the Map and see what a missing key returns.

get() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful