• JavaScript Video Tutorials

JavaScript - Map() constructor



In JavaScript, a Map is a built-in object that allows us to store 'key-value' pairs where both keys and the values can be of any data type. In a map, two or more keys cannot be same or identical, which means all the keys have to be unique.

The Map() constructor is used to create a new Map object. It can be invoked with the new keyword. When invoked with the new keyword, it creates a new Map object. If no arguments are provided, an empty Map object is created.

The Map() constructor can only be constructed with new keyword. If we attempt to invoke it without 'new', throws a TypeError.

Syntax

Following is the syntax of JavaScript Map() constructor −

new Map()
new Map(iterable)

Parameters

This constructor accepts an optional parameter. The same is described below −

  • iterable − An iterable object (such as an array or another Map) containing key-value pairs. Each key-value pair is an array with two elements: the key and the value.

Return value

A new Map object will be returned after initialising the Map constructor.

Examples

Example 1

In the following example, we are creating a new Map object using the Map() constructor −

<html>
<body>
   <script>
      const myMap = new Map([
         [1, 'apple'],
         [2, 'banana'],
         [3, 'cherry']
      ]);
   </script>
</body>
</html>

The above Map object contains three key-value pairs. The keys are '1', '2', and '3', and the corresponding values are 'apple', 'banana', and 'cherry', respectively.

Example 2

We can also add key-value pairs to a Map object after it has been created using the set() method as shown in the below example −

<html>
<body>
   <script>
      const myMap = new Map();
      myMap.set(1, 'apple');
      myMap.set(2, 'banana');
      myMap.set(3, 'cherry');
   </script>
</body>
</html>

The "myMap" object will contain the provided three key-value pairs as elements.

Example 3

We can access the value associated with a key in a Map object using the get() method as shown in the following example −

<html>
<body>
   <script>
      const myMap = new Map();
      
      myMap.set(1, 'apple');
      myMap.set(2, 'banana');
      myMap.set(3, 'cherry');
      
      document.write(myMap.get(1), "<br>");
      document.write(myMap.get(2), "<br>");
      document.write(myMap.get(3));
   </script>
</body>
</html>

The above program will print the "values" associated with the keys '1', '2', and '3' in the Map object.

Example 4

We can also check if a particular key exists in a Map object using the has() method as shown in the below example −

<html>
<body>
   <script>
      const myMap = new Map();      
      myMap.set(1, 'apple');
      myMap.set(2, 'banana');
      myMap.set(3, 'cherry');      
      document.write(myMap.has(2));
   </script>
</body>
</html>

It returns 'true' because the key '2' exists in the Map object.

Example 5

In this example, we are using the delete() method to delete a key-value pair from a Map object −

<html>
<body>
   <script>
      const myMap = new Map();
      myMap.set(1, 'apple');
      myMap.set(2, 'banana');
      myMap.set(3, 'cherry');
      document.write(myMap.delete(2), "<br>"); //true
      document.write(myMap.has(2)); //false
   </script>
</body>
</html>

The above program removes the key-value pair associated with the key '2' from the Map object.

Advertisements