Set

Set is a collection of unique values of any data type. The values in a Set are arranged in insertion order. A Set is created using the Set constructor. Here is an example:

const set1 = new Set();const set2 = new Set("Hello!!!");

Here set1 is an empty Set, whereas set2 was created using values of an iterable object, that is, the characters of a string and the string, was not empty; therefore, set2 is non-empty. The following example code demonstrates various operations that can be done on a Set:

let set = new Set("Hello!!!");set.add(12); //add 12console.log(set.has("!")); //check if value existsconsole.log(set.size);set.delete(12); //delete 12console.log(...set);set.clear(); //delete all values

The output is as follows:

true6H e l o ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.