A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.
Ways to create a set in js−
1. Using empty Set constructor
let mySet = new Set(); mySet.add(1); mySet.add(1); console.log(mySet)
Output
Set { 1 }
2. Passing an iterable to the constructor
The set constructor accepts an iterable object(list, set, etc) using which it constructs a new set.
Example
let mySet = new Set([1, 2, 1, 3, "a"]); mySet.add(1); mySet.add(1); console.log(mySet)
Output
Set { 1, 2, 3, 'a' }