Tuesday, February 28, 2023
HomeSoftware DevelopmentSet vs Map in JavaScript

Set vs Map in JavaScript


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

In JavaScript, Set and Map are two kinds of objects which might be used for storing information in an ordered method. Each these information constructions are used to retailer distinct kinds of information inside the identical object. In Maps, the info is saved as a key-value pair whereas in Set information is a single assortment of values which might be distinctive.

Let’s be taught in regards to the makes use of of those two information constructions.

JavaScript Set: It’s a assortment of values that may be accessed with no particular key. The weather are distinctive and the addition of duplicates will not be allowed. Units are principally used to take away duplicates from some other information construction

 

 

Syntax:

new Set([it]);

Instance: On this instance, we are going to discover ways to implement a set

Javascript

var pattern = new Set();

pattern.add("Hey");

pattern.add(1)

pattern.add("Bye")

pattern.add("@");

  

for (var merchandise of pattern) {

    console.log(merchandise);

    }

Output:

Hey
1
Bye
@

JavaScript Map: Maps are used to retailer information in key-value pairs the place keys are used to uniquely establish a component and values comprise the info related to it.

Syntax:

new Map([it])

Instance: On this instance, we are going to implement a map.

Javascript

var pattern = new Map();

pattern.set("identify", "Ram");

pattern.set("Function", "SDE")

pattern.set("Nation", "India")

  

for (var merchandise of pattern) {

    console.log(merchandise);

    }

Output:

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

What to make use of?

Selecting one information construction amongst these two depends upon the utilization and the way we wish to entry the info. If we wish the info to be recognized by a key then we must always go for maps but when we wish distinctive values to be saved then we must always use a set.

Map Set
It’s a assortment of key-value It’s a assortment of distinctive parts
Map is two-dimensional The set is one dimensional
Values are accessed utilizing keys In-built strategies are used to entry  values

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments