JavaScript Map is a collection of elements where each element is stored as a key, value pair. Map objects can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, and value pair in the same order as inserted.
Syntax:
Map.function()
Example: Below is an example of a Map.forEach() method.
Javascript
// Creating a map using Map object let mp= new Map() // Adding values to the map mp.set( "a" ,1); mp.set( "b" ,2); mp.set( "c" ,3); // Logging map object to console mp.forEach((values,keys)=>{ console.log(values,keys) }) |
Output:
1a 2b 3c
The complete list of JavaScript Map is listed below:
JavaScript Map Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.
Constructor |
Description |
Example |
---|---|---|
Map() | Create Map objects in JavaScript. |
JavaScript Map Properties: A JavaScript property is a member of an object that associates a key with a value.
- Instance Properties: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties |
Description |
Example |
---|---|---|
constructor | It is used to return the constructor function of Map. | |
size | Return the number of keys, and value pairs stored in a map. |
JavaScript Map Methods: JavaScript methods are actions that can be performed on objects.
- Instance Methods: If the method is called on the instance of a Map then it is called an instance method of Map.
Static Methods |
Description |
Example |
---|---|---|
clear( ) | Removal of all the elements from a map and making it empty. | |
delete() | Delete the specified element among all the elements which are present in the map. | |
entries( ) | Returning an iterator object which contains all the [key, value] pairs of each element of the map. | |
forEach() | The map with the given function executes the given function over each key-value pair. | |
get( ) | Returning a specific element among all the elements which are present in a map. | |
has( ) | Check whether an element with a specified key exists in a map or not. | |
keys() | The keys from a given map object return the iterator object of keys. | |
set() | Add key-value pairs to a Map object. | |
values() | Return a new Iterator object that contains the value of each element present in Map. |