JavaScript new Map()
Examples
Creating a Map object by passing an array to the new Map()
constructor:
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
Try it Yourself »
Creating a new Map object and add elements with the set()
method:
// Create a Map
const fruits = new Map();
// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
Try it Yourself »
Description
The new Map()
constructor creates a Map object.
Note
A Map object can only be constructed with new Map()
Syntax
new Map(iterable)
Parameters
Parameter | Description |
iterable | Optional. An iterable object with key-value pairs. |
Return Value
Type | Description |
Object | A new Map object. |
Browser Support
Map
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
Map
is not supported in Internet Explorer.