JavaScript Maps


A map stores key-value pairs in which the keys may be of any sort of data. The keys' original placement order is retained by a Map.

Method Description
new Map() Generates a new Map.
set() Adds a new key-value pair to the Map object.
get() Returns the value associated with a specified key.
delete() Removes the key-value pair associated with the specified key from the Map object.
has() Returns a boolean indicating whether the specified key exists in the Map object.
forEach() Each key/value pair in a Map is called by a different function.
entries() Returns an iterator for the key-value pairs in the Map object.
Property Description
size Returns the number of key-value pairs in the Map object.

Create map method

You can create a JavaScript Map by:

  1. Giving a new Map an Array ().
  2. Create a map using Map.set ().

new Map() Method

The new Map() method in JavaScript is used to generate a new Map object, which is a collection of key-value pairs with any type of key, including objects and functions, as a possible value.

Example

Preview

set() Method

The Map object's built-in set() function in JavaScript allows you to add new key-value pairs to the map or change the value of an existing key.

Example

Preview

get() Method

The get() function of the Map object in JavaScript is a built-in method that obtains the value connected to a certain key in the map.

Example

Preview

delete() Method

The delete() function of the Map object in JavaScript removes the key-value pair connected to a specified key from the map.

Example

Preview

has() Method

The has() function of the Map object in JavaScript is a built-in method that returns a boolean indicating whether or not a given key is present in the map.

Example

Preview

forEach() Method

The forEach() method of the Map object in JavaScript enables you to repeatedly loop over all the key-value pairs in the map and run a callback function for each pair.

Example

Preview

entries() Method

The forEach() method of the Map object in JavaScript enables you to repeatedly loop over all the key-value pairs in the map and run a callback function for each pair.

Example

Preview

size Property

The size property of the Map object in JavaScript is a built-in property that returns the number of key-value pairs in the map.

Example

Preview