AppDividend
Latest Code Tutorials

Javascript Set Example | Sets in Javascript

2

Javascript Set is an inbuilt object that lets you store the unique values of any type, whether there are primitive values or object references. The primary condition of the values should be in one Set is that value in a Set has to be unique, the value equality will be checked.

Sets are the new object type with the ES6 (ES2015) that allows creating collections of unique values. The values in the Set can be either simple primitives like strings or integers, but more complex nonprimitive types like object literals or arrays can also be part of a Set.

Getting Started With Sets In Javascript

The syntax of Set is the following.

new Set([iterable]);

If an iterable object is passed then all of its elements will be added to a new Set. If you don’t specify this parameter or fail to specify the parameter, or its value is null, then the new Set is empty.

#Add the values in the Set

Let us take an example of Sets in Node.js.

// server.js

const ASet = new Set();

ASet.add('Cream');

console.log(ASet);

Output

Getting Started With Sets In Javascript

Also, we can add the object inside the Set.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet);

Now, check the values in the Set using the following code.

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet.has('Cream')); // true

So, you can call the has method on Set to find whether the value is true or false.

#Iterating Sets

We can iterate the sets using the for-of loop. Let us take the for-of loop example.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet)
  console.log(set);

We can also iterate the keys.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet.keys())
  console.log(set);

Now iterate the values.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

for(set of ASet.values())
  console.log(set);

#Iterate the Set values in the forEach

We can iterate the Set values in the forEach function using the following code.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('12');

ASet.forEach(value => console.log(value));

#Clear the Set values

We can clear the Set values using the following command.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.clear();
console.log(ASet);

Output

Sets In JavaScript

#Check the length of Set

We can check the length of a Set using the size method.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});

console.log(ASet.size);

#Remove the value from Set

We can delete the particular value from Set using the delete() method.

// server.js

const ASet = new Set();

ASet.add('Cream');
ASet.add({
  'name': 'Krunal Lathiya',
  'designation': 'Web Developer And Author'
});
ASet.add('2');

console.log(ASet);

ASet.delete('2');

console.log(ASet);

Output

JavaScript Sets Example Tutorial

So, we have passed two as an argument to remove that item from the Set.

Finally, Javascript set example is over.

2 Comments
  1. Amit Parmar says

    thanks for the guide

  2. Amit Parmar says

    thanks for the post

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.