Map.prototype.set()

Adds or updates a key/value pair in a Map.

Since ES2015 (ES6) Spec ↗

Syntax

map.set(key, value)

Returns

Map — The `Map` itself, allowing chained calls.

Examples

const m = new Map();
m.set("a", 1).set("b", 2);
console.log(m.get("a"), m.get("b"));
Output
1 2
const m = new Map();
const key = {};
m.set(key, "obj value");
console.log(m.get(key));
Output
obj value

Notes

- Any value, including objects and functions, can be a key. - Returns the map, so `set()` calls can be chained. - Keys are compared with SameValueZero (so `NaN` matches `NaN`).

Browser & runtime support

EnvironmentSince version
chrome 38
firefox 13
safari 8
edge 12
node 0.12

See also