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)
Call
What 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
Parameter
Description
key
Required. Any value: a string, number, boolean, object, function, or NaN. Matching uses SameValueZero.
value
Required. Any value: a number, a string, an object, a function, even undefined.
Return Value
Return Value
Description
Map
The 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.
NaN can be a key.tags.set(NaN, "missing") stores one entry. Reading with get(NaN) finds it.
SameValueZero matches NaN.Number("x") is also NaN, so it finds the same entry. Size stays 1.
Replace, do not grow. Setting NaN again replaces the value with "updated". Size is still 1.
Compatibility
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 Chrome36+ · Desktop & Mobile
Full support
Mozilla Firefox6+ · Desktop & Mobile
Full support
Apple Safari8+ · macOS & iOS
Full support
Microsoft Edge12+
Full support
Node.js0.12+ · every current release
Supported
DenoAll releases
Supported
Internet ExplorerIE 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.