Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Map Complete Reference

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 17 Mar, 2023
Improve Article
Save Article

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




<script>
    // 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)
    })
</script>

Output:

1a
2b
3c

 

 

The complete list of JavaScript Map are listed below:

JavaScript Map Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.

Constructor

Description

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

    constructorIt is used to create a map object.
    sizeReturn the number of keys, and value pairs stored in a map.

JavaScript Map Methods: JavaScript methods are actions that can be performed on objects.

  • Static Methods: If the method is called using the Number class itself then it is called a static method.

    Static Methods

    Description

    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.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!