JS: Map (Key Value List)

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

What is Map Object

The map object is designed to be used as a map data structure.

Create Map

new Map()
Create a empty map.
new Map(iterable)
Create a map from values in Iterable Object iterable.
const xx = new Map();

// add new item
xx.set(1, "n1");
xx.set(2, "n2");

console.log(xx);
// Map { 1 => "n1", 2 => "n2" }

Parent of Map Objects

The parent of any map object is Map.prototype.

Add or Modify Entry

Delete Entry

Check Key Existence

Get Key's Value

Size of Map

Clear Map

Iterate Map

Get All Keys

Get All Values

Add Properties to Map

Because map is a object, you can add object properties to it, but you shouldn't do it.

const xx = new Map([[3, "n3"], [4, "n4"], [5, "n5"]]);

// you can add properties to a map object
xx.pp = 22;
console.log(xx.pp === 22);

JavaScript. Map Object

Key value data type in programing languages