Map.prototype.set()
Adds or updates a key/value pair in a Map.
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
| Environment | Since version |
|---|---|
| chrome | 38 |
| firefox | 13 |
| safari | 8 |
| edge | 12 |
| node | 0.12 |