JavaScript Set: The Complete Guide

2
30
JavaScript Sets Data Structure

Sets are the new object type with the ES6 (ES2015) that allows the creation of collections of unique values. The sets’ values can be simple primitives like strings or integers, but more complex non-primitive types like object literals or arrays can also be part of a Set.

JavaScript Set

Javascript Set is a built-in 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 the value in a Set has to be unique, and the value equality will be checked.

Syntax

The syntax of Set is the following.

new Set([iterable]);

Parameters

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

Adding the values in the Set

To add a new element in the Set, use the add() method. Let us take an example of Set 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.

How to Iterate Set in JavaScript

To iterate a Set in JavaScript, use the for loop. The for loop has three statements: The first statement is executed (one time). before the execution of the code block. The second statement defines the condition for executing the code block. Finally, the third statement is executed (every time) after the code block has been executed.

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);

How to iterate the Set values in the forEach

To iterate the set values in JavaScript, use the forEach() function. The forEach() is a built-in JavaScript function that executes the provided function once for each set element.

// 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));

How to clear the Set in JavaScript

To clear the set values in JavaScript, use the Set.clear() method. The Set.clear() is a built-in JavaScript method used to remove all the elements from a set and make it empty.

// 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

How to check the length of a Set in JavaScript

To find the length of a Set in JavaScript, use the Set.size property. The size is a built-in JavaScript accessor property that returns the number of (unique) elements in a Set object.

// server.js

const ASet = new Set();

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

console.log(ASet.size);

Removing an element from Set in JavaScript

To remove an element from a Set in JavaScript, use the delete() method. The delete() is a built-in JavaScript method that removes a specified value from a Set object if it is in the Set.

// 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.

That’s it for Set in JavaScript.

2 Comments

Leave A Reply

Please enter your comment!
Please enter your name here

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