What is Map.keys method in JavaScript?
The keys method of the
Syntax
mapObj.keys()
Returns
The keys() method will return an iterator object. The returned iterator object contains all the keys of the map in the insertion order.
Code
let map = new Map();map.set("one", 1);map.set("two", 2);map.set("three", 3);let keyIterator = map.keys();for(let key of keyIterator){console.log(key)}
Explanation
In the code above:
-
We have created a
Mapobject and added some entries to it. -
Then called the
keysmethod to get all the keys of the map objects. -
Then printed the iterator object returned by the
keysmethod using thefor...ofloop.