Map.prototype.set()
The set()
method adds or updates an element with a specified key and a
value to a Map
object.
Syntax
set(key, value)
Parameters
key
-
The key of the element to add to the
Map
object. value
-
The value of the element to add to the
Map
object.
Return value
The Map
object.
Examples
Using set()
let myMap = new Map()
// Add new elements to the map
myMap.set('bar', 'foo')
myMap.set(1, 'foobar')
// Update an element in the map
myMap.set('bar', 'baz')
Using the set() with chaining
Since the set()
method returns back the same Map
object, you can chain the
method call like below:
// Add new elements to the map with chaining.
myMap.set('bar', 'foo')
.set(1, 'foobar')
.set(2, 'baz');
Specifications
Specification |
---|
ECMAScript Language Specification # sec-map.prototype.set |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
set | ChromeFull support38 | EdgeFull support12 | FirefoxFull support13 | Internet ExplorerPartial support11 | OperaFull support25 | SafariFull support8 | WebView AndroidFull support38 | Chrome AndroidFull support38 | Firefox for AndroidFull support14 | Opera AndroidFull support25 | Safari on iOSFull support8 | Samsung InternetFull support3.0 | DenoFull support1.0 | Node.jsFull support0.12.0 |
Legend
- Full support
- Full support
- Partial support
- Partial support
- No support
- No support
- See implementation notes.
- User must explicitly enable this feature.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.