In this section, we will learn what the keys() method is and how to use it in JavaScript.
What is JavaScript Map keys() Method?
We use the `keys()` method to get an iterator object that contains all the keys of the target map object.
Note: the iterator object is explained in later sections.
We can then use this method and the for-of statement to loop through all the keys of the target map object.
JavaScript Map keys() Method Syntax:
keys()
keys() Function Parameters:
The method does not take an argument.
keys() Function Return Value:
The return value of this method is an iterator object that contains all the keys of the target map object.
Example: using Map keys() method in JavaScript
const map = new Map(); map.set("John","222-22222222"); map.set(22 , 2000); map.set(true, false); map.set("Jack", "333-3234232"); for (const key of map.keys()){ console.log(key); }
Output:
John 22 true Jack