Horje
What is Set Constructor in JavaScript ?

In JavaScript, the Set constructor is used to create a new Set object. A Set is a built-in object in JavaScript that allows you to store unique values, whether they are primitive values or object references. The Set constructor is called with the new keyword to create an instance of a Set.

Syntax:

let mySet = new Set([iterable]);

Parameters:

  • mySet: The variable that will reference the newly created Set instance.
  • iterable (optional): An iterable (such as an array) whose elements are used to initialize the Set. If not provided, an empty Set is created.

Example: In this example, we will see that the 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 } }



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is Splice in JavaScript ? What is Splice in JavaScript ?
Babylon.js Babylon.js
Remove Duplicate Elements from TypeScript Array Remove Duplicate Elements from TypeScript Array
How to Iterate Over Characters of a String in TypeScript ? How to Iterate Over Characters of a String in TypeScript ?
TypeScript Array Symbol.iterator Method TypeScript Array Symbol.iterator Method

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