JavaScript Map set()

Definition and usage

set() The method is used to add elements to the Map.

set() The method can also be used to update elements in the Map.

Instance

Example 1

You can use set() The method adds elements to the Map:

// Create a Map
const fruits = new Map();
// Set the value of the Map
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);

Try it yourself

Example 2

set() The method can also be used to change the existing values in the Map:

fruits.set("apples", 500);

Try it yourself

Syntax

map.set(key, value)

Parameters

Parameters Description
key Required. The key of the element.
value Required. The value of the element.

Return value

Type Description
Map The Map object itself.

Browser support

map.set() Is a feature of ECMAScript6 (ES6).

Starting from June 2017, all modern browsers support ES6 (JavaScript 2015):

Chrome Edge Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 April 2017 June 2017 September 2016 June 2016

map.set() Not Supported in Internet Explorer.