What is Map.keys method in JavaScript?

The keys method of the MapThe Map object is a collection of key-value pairs object will return the keys of all the map entries.

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 Map object and added some entries to it.

  • Then called the keys method to get all the keys of the map objects.

  • Then printed the iterator object returned by the keys method using the for...of loop.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources