Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
Warning: The SpiderMonkey Set implementation is a prototype and the Set API and semantics specifications are unstable. The SpiderMonkey implementation may not reflect the latest specification draft. It is subject to change any time. It is provided as an experimental feature. Do not rely on it for production code.
Set objects let you store unique values of any type, whether primitive values or object references. Value equality is 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 are different values. NaN can also be stored in a Set.
API
| Constructor | Description |
|---|---|
new Set([iterable]) |
Returns a new Set object. If an iterable object is passed, all of its elements will be added to the new Set. |
| Method | Description |
mySet.add(value) |
Adds the value to mySet. Returns undefined. |
mySet.delete(value) |
Sets the value for the key in mySet. Returns true when the value could be deleted, false otherwise. |
mySet.has(value) |
Returns a boolean asserting whether the value has been added to mySet or not. |
mySet.clear() |
Firefox 19Removes all key/value pairs from mySet. |
| Property | Description |
mySet.size |
Returns the number of key/value pairs in size was a method. In Firefox 19 and later it is a property. |
Examples
var mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");
mySet.has(1); // true
mySet.has(3); // false, 3 has not been added to the set
mySet.has(5); // true
mySet.has(Math.sqrt(25)); // true
mySet.has("Some Text".toLowerCase()); // true
mySet.size; // 3
mySet.delete(5); // removes 5 from the set
mySet.has(5); // false, 5 has been removed
mySet.size; // 2, we just removed one value
// iterate over items in set
for (let item of mySet) console.log(item); // logs the items in the order: 1, "some text"
// convert set to plain Array
var myArr = [v for (v of mySet)]; // [1, "some text"]
// the following will also work if run in an HTML document
mySet.add(document.body);
mySet.has(document.querySelector("body")); // true
// converting between Set and Array
mySet2 = Set([1,2,3,4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]
// intersect can be simulated via Set(x for (x of set1) if (set2.has(x)))
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | Not supported | 13.0 (13) | Not supported | Not supported | Not supported |
| iterable | Not supported | 17.0 (17) | Not supported | Not supported | Not supported |
| Set.clear() | Not supported | 19.0 (19) | Not supported | Not supported | Not supported |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | Not supported | 13.0 (13) | Not supported | Not supported | Not supported |
| iterable | Not supported | 17.0 (17) | Not supported | Not supported | Not supported |
| Set.clear() | Not supported | 19.0 (19) | Not supported | Not supported | Not supported |
Mozilla Developer Network