JavaScript Map() Constructor
The Map() constructor is used to create Map objects in JavaScript. The map is a data structure that stores elements as a key, value pair.
Syntax:
new Map() new Map(iterable)
Parameters:
- iterable: An iterable object used for iterating through elements, stored as a key, value pair.
Return value: A new Map object returns after the initialization of the map constructor.
Example 1:
Javascript
<script> // map1 contains // 1 => 10 // 2 => 20 // 3 => 30 // 4 => 40 var map1 = new Map([ [1 , 10], [2 , 20] , [3, 30],[4, 40] ]); console.log("Map1: "); console.log(map1);</script> |
Output:
Map1:
Map(4) { 1 => 10, 2 => 20, 3 => 30, 4 => 40 }Example 2:
Javascript
<script> // map2 contains // firstname => Ram // lastname => Prasad // website => geeksforgeeks var map2 = new Map([ ["firstname" ,"Ram"], ["lastname", "Prasad"], ["website", "geeksforgeeks"] ]); console.log("Map2: "); console.log(map2);</script> |
Output:
Map2:
Map(3) {
'firstname' => 'Ram',
'lastname' => 'Prasad',
'website' => 'geeksforgeeks'
}Example 3:
Javascript
<script> // Map contains nested array var map3 = new Map([ ["whole numbers", [1 ,2 ,3 ,4]], ["Decimal numbers" , [1.1, 1.2, 1.3, 1.4]], ["negative numbers", [-1, -2, -3, -4]] ]); console.log("Map3: "); console.log(map3);</script> |
Output:
Map3:
Map(3) {
'whole numbers' => [ 1, 2, 3, 4],
'Decimal numbers' => [ 1.1, 1.2, 1.3, 1.4],
'negative numbers' => [ -1, -2, -3, -4]
}Supported Browsers:
- Chrome 38 and above
- Edge 12 and above
- Firefox 13 and above
- Internet Explorer 11 and above
- Opera 25 and above
- Safari 8 and above
We have a complete list of Javascript Map methods, to check those please go through this JavaScript MapComplete Reference article.




.gif)

Please Login to comment...