JS: Map Object

By Xah Lee. Date: . Last updated: .

New in ES2015.

Map is the value of the property key "Map" of the global object. [see JS: the Global Object]

console.log( window.Map === Map ); // true


Map is a function.

[see JS: Value Types]

console.log ( typeof Map === "function" ); // true


Parent of Map is Function.prototype. [see JS: Prototype and Inheritance]

console.log ( Reflect.getPrototypeOf ( Map ) === Function.prototype ); // true


Purpose of Map is:

  1. To create instance of “map” object. Instance of map object is a collection of key/value pairs as a lookup table data structure. This is different from the generic JavaScript object 〔JS: Object Type 〕. The map object instance is designed specifically as a lookup table data structure.
  2. Used as a namespace to hold general purpose methods for working with “map” instances.
  3. Holds the property key "prototype". The value of Map.prototype is the parent object of all “map” instances.

Facts about map:

[see JS: the Map Object Tutorial]

How Map Determines Uniqueness of Keys

The equality test used for determining whether 2 keys in a map is the same as ===, except treatment of NaN (not a number).

NaN === NaN return false, but for map object, NaN is considered same as any NaN.

// this is false
console.log(NaN === NaN); // false

// for map object, NaN is same as any NaN
const m = new Map();
m.set(NaN, "n1");
m.set(NaN, "n2");

console.log(m) // Map { NaN => 'n2' }


JS: Map Constructor


JS Map

  1. the Map Object Tutorial
  2. Iterate Over Map Object
  3. Convert Object to/from Map
  4. Map Filter
  5. Reverse Key/Value

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials