Set

This is an experimental technology, part of the Harmony (ECMAScript 6) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

Summary

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.

Description

Set objects are collections of values, you can iterate its elements 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 and 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. However, this has been changed in the latest ECMAScript 6 specification. Starting with Gecko 29.0 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26) (bug 952870) and a recent nightly Chrome, +0 and -0 are treated as the same value in Set objects. Also, NaN and undefined can also be stored in a Set.

Properties

Set.length
The value of the length property is 0.
Set.prototype
Represents the prototype for the Set constructor. Allows the addition of properties to all Set objects.

Set instances

All Set instances inherit from Set.prototype.

Properties

Set.prototype.constructor
Returns the function that created an instance's prototype. This is the Set function by default.
Set.prototype.size
Returns the number of values in the Set object.

Methods

Set.prototype.add(value)
Appends a new element with the given value to the Set object.
Set.prototype.clear()
Removes all elements from the Set object.
Set.prototype.delete(value)
Removes the element associated to the value. Set.prototype.has(value) will return false afterwards.
Set.prototype.entries()
Returns 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.
Set.prototype.forEach(callbackFn[, thisArg])
Calls callbackFn once for each value present in the Set object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
Set.prototype.has(value)
Returns a boolean asserting whether an element is present with the given value in the Set object or not.
Set.prototype.keys()
Is the same function as the values() function and returns a new Iterator object that contains the values for each element in the Set object in insertion order.
Set.prototype.values()
Returns returns a new Iterator object that contains the values for each element in the Set object in insertion order.

Examples

Example: Using the Set object

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

Example: Iterating Sets

// iterate over items in set
// logs the items in the order: 1, "some text" 
for (let item of mySet) console.log(item);

// logs the items in the order: 1, "some text" 
for (let item of mySet.keys()) console.log(item);
 
// logs the items in the order: 1, "some text" 
for (let item of mySet.values()) console.log(item);

// logs the items in the order: 1, "some text" (here, key and value are same)
for (let [key, value] of mySet.entries()) console.log(key);

// 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  
var intersection = new Set([for (x of set1) if (set2.has(x)) x]);

// NOTE: SpiderMonkey does not yet support the ES6 Array Comprehension syntax shown here 


// Iterate set entries with forEach
mySet.forEach(function(value) {
  console.log(value);
});

// 1
// 2
// 3
// 4

Specifications

Specification Status Comment
ECMAScript Language Specification 6th Edition (ECMA-262) Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 31 [1] 13 (13) 11 Not supported Not supported
iterable Not supported 17 (17) Not supported Not supported Not supported
Set.clear() 31 [1] 19 (19) 11 Not supported Not supported
Set.keys(), Set.values(), Set.entries() Not supported 24 (24) Not supported Not supported Not supported
Set.forEach() Not supported 25 (25) 11 Not supported Not supported
Value equality for -0 and 0 (Yes) 29 (29) ? 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
Set.keys(), Set.values(), Set.entries() Not supported 24.0 (24) Not supported Not supported Not supported
Set.forEach() Not supported 25.0 (25) Not supported Not supported Not supported
Value equality for -0 and 0 Not supported 29.0 (29) Not supported Not supported Not supported

Chrome-specific notes

  • [1] The feature is available behind a preference. In chrome://flags, activate the entry “Enable Experimental JavaScript”.

See also

Document Tags and Contributors

Last updated by: fscholz,