JavaScript Maps

A map can store key-value pairs with any data type for the keys.

The keys’ original placement order is retained by a Map.

MethodDescription
Map()Creates a new Map object.
clear()Removes all key/value pairs from the Map object.
delete(key)Removes the key/value pair with the specified key.
entries()Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
forEach((value, key, map) => {…})Calls a function for each key/value pair in the Map object.
get(key)Returns the value associated to the key, or undefined if there is none.
has(key)Returns a boolean indicating whether an element with the specified key exists or not.
keys()Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
set(key, value)Sets the value for the key in the Map object. Returns the Map object.
values()Returns a new Iterator object that contains the values for each element in the Map object in insertion order.

Example code:

Console Output:

John

 false

Leave a Comment

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

Scroll to Top