JavaScript Map

Maps are used to store data in key-value pairs where keys are used to uniquely identify an element and values contain the data associated with it.

Syntax:

new Map([it]);

Example: In this example, we will implement a map.

Javascript




let sample = new Map();
sample.set("name", "Ram");
sample.set("Role", "SDE")
sample.set("Country", "India")
 
for (let item of sample) {
    console.log(item);
    }


Output

[ 'name', 'Ram' ]
[ 'Role', 'SDE' ]
[ 'Country', 'India' ]

Set vs Map in JavaScript

In JavaScript, Set and Map are two types of objects that are used for storing data in an ordered manner. Both these data structures are used to store distinct types of data inside the same object. In Maps, the data is stored as a key-value pair whereas in Set data is a single collection of values that are unique.

Let’s learn about the uses of these two data structures.

Similar Reads

JavaScript Set

It is a collection of values that can be accessed without a specific key. The elements are unique and the addition of duplicates is not allowed. Sets are mostly used to remove duplicates from any other data structure...

JavaScript Map

...

What to use?

Maps are used to store data in key-value pairs where keys are used to uniquely identify an element and values contain the data associated with it....

Difference between Map and set

...

Contact Us