The Set object lets you store unique values of any type, whether primitive values or object references.

Syntax

new Set([iterable]);

Parameters

iterable

If an iterable object is passed, all of its elements will be added to the new Set. null is treated as undefined.

Description

Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.

Value equality

Because each value in the Set has to be unique, the value equality will be checked. In an earlier version of ECMAScript specification this was not based on the same algorithm as the one used in the === operator. Specifically, for Sets, +0 (which is strictly equal to -0) and -0 were different values. However, this was changed in the ECMAScript 2015 specification. See "Value equality for -0 and 0" in the browser compatability table for details.

Also, NaN and undefined can also be stored in a Set. NaN is considered the same as NaN (even though NaN !== NaN).

Returns a new Iterator object that contains an array of [value, value] for each element in the Set object, in insertion order. This is kept similar to the Map object, so that each entry has the same value for its key and value here.

Relation with Array objects

var myArray = ["value1", "value2", "value3"];
// Use the regular Set constructor to transform an Array into a Set
var mySet = new Set(myArray);
mySet.has("value1"); // returns true
// Use the spread operator to transform a set into an Array.
console.log([...mySet]); // Will show you exactly the same Array as myArray