JavaScript – Map – DEV Community

Hey πŸ‘‹, hope y’all are properly! Lets discuss Map in JavaScript!

Map πŸ—ΊοΈ

Map in JavaScript (JS) can help you retailer info/knowledge utilizing a key-value relationship. However wait… is not that the identical as an Object in JS? Map does return Object while you use typeof however it isn’t precisely the identical. They’re very comparable in nature however they’ve distinct variations that decide their makes use of.

So what are these distinct variations? Effectively, in case you are utilizing Object the keys have to be a String or Image datatype whereas the keys in Map could be any datatype together with Operate and Object. One other factor is that to seek out the scale of an Object it’s essential to do it manually, e.g. grabbing all of the keys utilizing Object.keys() then counting the size. However, Map has a property .dimension which return the scale of the Map.

Moreover, Objects will not be straight iterable whereas with Maps you may.

Instance: Map comes with its personal strategies and you’ll see a few of them under.

const cypher = {identify: "Cypher", coloration: "white"};
const raze = {identify: "Raze", coloration: "orange"};
const omen = {identify: "Omen", coloration: "blue"};

const characterRoles = new Map(); // create an empty Map.

// add new knowledge

characterRoles.set(cypher, "sentinel");

// you may chain the tactic so as to add a number of entries
characterRoles.set(raze, "duelist")
   .set(omen, "smokes");

// test dimension
characterRoles.dimension // 3

// iterable
characterRoles.forEach((function, char) => {
    console.log(char.identify + " is a " + function)

// Cypher is a sentinel
// Raze is a duelist
// Omen is a smokes
Enter fullscreen mode

Exit fullscreen mode

As I proceed to be taught extra about Information Construction and Algorithm, I learnt that Map is JavaScript’s equal to a Hashmap and have been utilizing them to unravel some issues.


To summarise, Map can help you retailer knowledge as key-value pairs that aren’t restricted to String (or Image) datatype as keys. It additionally provide the potential to straight iterate by means of the information. Moreover, Maps have it is personal properties and strategies that you should use so as to add, entry, edit, and delete the information within the assortment.

Thanks for studying this brief publish, please depart a remark if you wish to add info or give suggestions πŸ˜€.

Add a Comment

Your email address will not be published. Required fields are marked *