Horje
JavaScript Set() Constructor

The Javascript Set constructor is used to create Set objects. It will create a set of unique values of any type, whether primitive values or object preferences. It returns the new Set object. It mainly helps in creating a new set of objects which contains unique elements.

Syntax:

new Set()
new Set(iterable)

Parameters:

This method accepts a single parameter that is described below:

  • iterable: If an object is passed, all values will be added to the new set. If the parameter is not specified then the new set is empty.

Return Value:

  • This method returns a new Set object.

Example 1: In this example, we will see how to create a new Set object.

JavaScript
const mySet = new Set();

mySet.add("California");
mySet.add("India");
mySet.add("Russia");
mySet.add(10);

const myObject = { a: 1, b: 8 };

mySet.add(myObject);

console.log(mySet);

Output:

Set(5) { 'California', 'India', 'Russia', 10, { a: 1, b: 8 } }

Example 2: In this example, we will see that set takes only a unique value.

JavaScript
const mySet = new Set();

mySet.add("California");
mySet.add("India");
mySet.add("California");
mySet.add(10);
mySet.add(10);

const myObject = { a: 1, a: 5 };

mySet.add(myObject);

console.log(mySet);

Output:

Set(4) { 'California', 'India', 10, { a: 5 } }

In this example, we have added two times “California” and 10 but it creates a set with unique values.

We have a complete list of Javascript Set methods, to check those please go through this Sets in JavaScript article.

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

JavaScript Set() Constructor- FAQs

What does the Set constructor do in JavaScript?

The Set constructor creates a new Set object, which is a collection of unique values. A Set object can hold any type of values, whether primitive or object references.

Can a Set contain duplicate values?

No, a Set cannot contain duplicate values. If you try to add a value that is already in the set, it will not be added again.

Can you use the Set constructor with WeakSet?

No, the Set constructor is for creating Set objects. WeakSet is a separate constructor for creating weak sets, which only accept objects as values.

How does the Set constructor handle initial values?

If you pass an iterable object (such as an array) to the Set constructor, each element of the iterable is added to the set. Duplicate elements in the iterable are ignored.

What is the most common use of the Set constructor?

Most Common Use Cases:

  • Removing duplicate values from an array.
  • Storing unique items like user IDs, tokens, or references.
  • Performing set operations like union, intersection, and difference.
  • Managing collections of data where uniqueness is required.


Reffered: https://www.geeksforgeeks.org


JavaScript

Related
When should we use curly braces for ES6 import ? When should we use curly braces for ES6 import ?
WeakSet vs WeakMap in JavaScript WeakSet vs WeakMap in JavaScript
How to display images from an array in JavaScript ? How to display images from an array in JavaScript ?
Next.js getInitialProps Next.js getInitialProps
JavaScript Date setYear() Method JavaScript Date setYear() Method

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11