• JavaScript Video Tutorials

JavaScript - Map.keys() Method



In JavaScript, the Map.keys() method does not accept any parameters and returns a new iterator object that contains the keys for each element present in the Map object, in insertion order. A Map object is a collection of key-value pairs where each key is unique and can be of any data type, and each value can also be of any data type.

This method is compatible in almost every browser such as Chrome, Edge, FireFox, Opera, and Safari.

Syntax

Following is the syntax of JavaScript Map.keys() method −

keys()

Parameters

This method does not accept any parameters.

Return value

This method returns a new Iterator object containing the keys of the Map in insertion order.

Examples

Example 1

In the following example, we are creating a Map, adding some key-value pairs, then iterating through the keys using Map.keys() method and printing them in insertion order using for...of loop from the result of the keys() method −

<html>
<body>
   <script>
      const map = new Map();
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
   
      const keysIterator = map.keys();
      for (let key of keysIterator) {
         document.write(key, "<br>");
      }
   </script>
</body>
</html>

The above program returns a new set iterator object "keysIterator" that contains the "key" for each element in this Map in insertion order.

Example 2

In this example, we're using the keys() method to create an iterator for the keys in the Map. Then, we are manually retrieving each key element using the next() method on the iterator −

<html>
<body>
   <script>
      const map = new Map();
      
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
      
      const iterator = map.keys();
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value);
   </script>
</body>
</html>

If we execute the above program, it prints each key present in the set, in insertion order.

Example 3

Here, the Map object is empty and we are trying to retrieve elements using the next() method on the iterator −

<html>
<body>
   <script>
      const map = new Map();
      const iterator = map.keys();
      document.write(iterator.next().value);
   </script>
</body>
</html>

It returns "undefined" as result.

Example 4

Here, the "iterator.next().done" will return "true", that indicates there are no more keys present −

<html>
<body>
   <script>
      const map = new Map();
      map.set('a', 'apple');
      map.set('b', 'banana');
      map.set('c', 'cherry');
      
      const iterator = map.keys();
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().value, "<br>");
      document.write(iterator.next().done);
   </script>
</body>
</html>

If we execute the above program, it prints each key present in the set, in insertion order and returns "true" as all the keys from the set are finished.

Advertisements