Favorites

JavaScript Map.prototype.set() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Map.prototype.set()
JavaScript Map Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The set() method adds or updates an entry in a Map and returns the Map, so calls can be chained.

Description

The set() method adds an entry or updates the value of an existing key. New keys are added at the end of the iteration order; updating a key keeps its position. Because it returns the map, you can chain calls.

Unlike object properties, map keys keep their type, so the number 1 and the string '1' are different keys. Using objects or DOM elements as keys is a common way to attach data to them, although a WeakMap is better when the keys might be removed, so the data doesn't keep them in memory.

Keys are compared with the SameValueZero algorithm: primitives by value, NaN as equal to itself, and objects only when they're the same object.

Syntax

map.set(key, value)

Parameters

  • keyThe key, of any type.
  • valueThe value to store.

Return Value

The same Map, so calls can be chained.

Example

const m = new Map();
m.set('x', 1).set('y', 2).set('x', 10);
console.log(m);

Browser Support

The following information will show you the current browser support for the Map.prototype.set() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee