Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1062336 of Set

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Set
  • Revision title: Set
  • Revision id: 1062336
  • Created:
  • Creator: stpn
  • Is current revision? No
  • Comment

Revision Content

{{JSRef}}

The Set object lets you store unique values of any type, whether {{Glossary("Primitive", "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 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 {{geckoRelease("29")}} ({{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. NaN is considered the same as NaN (even though NaN !== NaN).

Properties

Set.length
The value of the length property is 0.
{{jsxref("Set.@@species", "get Set[@@species]")}}
The constructor function that is used to create derived objects.
{{jsxref("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 {{jsxref("Set.prototype")}}.

Properties

{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}

Methods

{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}

Examples

Using the Set object

var mySet = new Set();

mySet.add(1);
mySet.add(5);
mySet.add("some text");
var o = {a: 1, b: 2};
mySet.add(o);

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.has(o); // true

mySet.size; // 4

mySet.delete(5); // removes 5 from the set
mySet.has(5);    // false, 5 has been removed

mySet.size; // 3, we just removed one value

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" 
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);

// convert set to plain Array (with Array comprehensions)
var myArr = [v for (v of mySet)]; // [1, "some text"]
// Alternative (with Array.from)
var myArr = Array.from(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 = new Set([1,2,3,4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]

// intersect can be simulated via 
var intersection = new Set([...set1].filter(x => set2.has(x)));

// difference can be simulated via
var difference = new Set([...set1].filter(x => !set2.has(x)));

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

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

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(uneval([...mySet])); // Will show you exactly the same Array as myArray

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-set-objects', 'Set')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-set-objects', 'Set')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support

{{ CompatChrome(38) }} [1]

{{ CompatGeckoDesktop("13") }} {{ CompatIE("11") }} 25 7.1
Constructor argument: new Set(iterable) {{ CompatChrome(38) }} {{ CompatGeckoDesktop("13") }} {{CompatNo}} 25 {{CompatNo}}
iterable {{ CompatChrome(38) }} {{ CompatGeckoDesktop("17") }} {{CompatNo}} 25 7.1
Set.clear() {{ CompatChrome(38) }} {{CompatGeckoDesktop("19")}} {{ CompatIE("11") }} 25 7.1
Set.keys(), Set.values(), Set.entries() {{ CompatChrome(38) }} {{CompatGeckoDesktop("24")}} {{CompatNo}} 25 7.1
Set.forEach() {{ CompatChrome(38) }} {{CompatGeckoDesktop("25")}} {{ CompatIE("11") }} 25 7.1
Value equality for -0 and 0 {{ CompatChrome(38) }} {{CompatGeckoDesktop("29")}} {{CompatNo}} 25 {{CompatNo}}
Constructor argument: new Set(null) {{CompatVersionUnknown}} {{CompatGeckoDesktop("37")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Monkey-patched add() in Constructor {{CompatVersionUnknown}} {{CompatGeckoDesktop("37")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Set[@@species] {{CompatUnknown}} {{CompatGeckoDesktop("41")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Set() without new throws {{CompatUnknown}} {{CompatGeckoDesktop("42")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatChrome(38)}} [1] {{ CompatGeckoMobile("13") }} {{CompatNo}} {{CompatNo}} 8
Constructor argument: new Set(iterable) {{CompatNo}} {{CompatChrome(38)}} {{ CompatGeckoMobile("13") }} {{CompatNo}} {{CompatNo}} {{CompatNo}}
iterable {{CompatNo}} {{CompatNo}} {{ CompatGeckoMobile("17") }} {{CompatNo}} {{CompatNo}} 8
Set.clear() {{CompatNo}} {{ CompatChrome(38) }} {{CompatGeckoMobile("19")}} {{CompatNo}} {{CompatNo}} 8
Set.keys(), Set.values(), Set.entries() {{CompatNo}} {{ CompatChrome(38) }} {{CompatGeckoMobile("24")}} {{CompatNo}} {{CompatNo}} 8
Set.forEach() {{CompatNo}} {{ CompatChrome(38) }} {{CompatGeckoMobile("25")}} {{CompatNo}} {{CompatNo}} 8
Value equality for -0 and 0 {{CompatNo}} {{ CompatChrome(38) }} {{CompatGeckoMobile("29")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Constructor argument: new Set(null) {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("37")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Monkey-patched add() in Constructor {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("37")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Set[@@species] {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile("41")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Set() without new throws {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile("42")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

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

See also

  • {{jsxref("Map")}}
  • {{jsxref("WeakMap")}}
  • {{jsxref("WeakSet")}}

Revision Source

<div>{{JSRef}}</div>

<p>The <strong><code>Set</code></strong> object lets you store <em>unique</em> values of any type, whether {{Glossary("Primitive", "primitive values")}} or object references.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
new Set([iterable]);</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt>iterable</dt>
 <dd>If an <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">iterable object </a>is passed, all of its elements will be added to the new Set. null is treated as undefined.</dd>
</dl>

<h2 id="Description">Description</h2>

<p><code>Set</code> objects are collections of values, you can iterate its elements in insertion order. A value in the <code>Set</code> may only occur once; it is unique in the <code>Set</code>'s collection.</p>

<h3 id="Value_equality">Value equality</h3>

<p>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 <code>Set</code>s, <code>+0</code> (which is strictly equal to <code>-0</code>) and <code>-0</code> are different values. However, this has been changed in the latest ECMAScript 6 specification. Starting with Gecko 29.0 {{geckoRelease("29")}} ({{bug("952870")}}) and a <a href="https://code.google.com/p/v8/issues/detail?id=3069">recent nightly Chrome</a>, <code>+0</code> and <code>-0</code> are treated as the same value in <code>Set</code> objects. Also, <code>NaN</code> and <code>undefined</code> can also be stored in a Set. <code>NaN</code> is considered the same as <code>NaN</code> (even though <code>NaN !== NaN</code>).</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt><code>Set.length</code></dt>
 <dd>The value of the <code>length</code> property is 0.</dd>
 <dt>{{jsxref("Set.@@species", "get Set[@@species]")}}</dt>
 <dd>The constructor function that is used to create derived objects.</dd>
 <dt>{{jsxref("Set.prototype")}}</dt>
 <dd>Represents the prototype for the <code>Set</code> constructor. Allows the addition of properties to all <code>Set</code> objects.</dd>
</dl>

<h2 id="Set_instances"><code>Set</code> instances</h2>

<p>All <code>Set</code> instances inherit from {{jsxref("Set.prototype")}}.</p>

<h3 id="Properties_2">Properties</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Properties')}}</p>

<h3 id="Methods">Methods</h3>

<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/Set/prototype','Methods')}}</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_the_Set_object">Using the <code>Set</code> object</h3>

<pre class="brush: js">
var mySet = new Set();

mySet.add(1);
mySet.add(5);
mySet.add("some text");
var o = {a: 1, b: 2};
mySet.add(o);

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.has(o); // true

mySet.size; // 4

mySet.delete(5); // removes 5 from the set
mySet.has(5);    // false, 5 has been removed

mySet.size; // 3, we just removed one value
</pre>

<h3 id="Iterating_Sets">Iterating Sets</h3>

<pre class="brush: js">
// 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" 
//(key and value are the same here)
for (let [key, value] of mySet.entries()) console.log(key);

// convert set to plain Array (with <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions">Array comprehensions</a>)
var myArr = [v for (v of mySet)]; // [1, "some text"]
// Alternative (with <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from">Array.from</a>)
var myArr = Array.from(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 = new Set([1,2,3,4]);
mySet2.size; // 4
[...mySet2]; // [1,2,3,4]

// intersect can be simulated via 
var intersection = new Set([...set1].filter(x =&gt; set2.has(x)));

// difference can be simulated via
var difference = new Set([...set1].filter(x =&gt; !set2.has(x)));

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

// 1
// 2
// 3
// 4</pre>

<h3 id="Relation_with_Array_objects">Relation with <code>Array</code> objects</h3>

<pre class="brush: js">
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(uneval([...mySet])); // Will show you exactly the same Array as myArray</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-set-objects', 'Set')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>
    <p>{{ CompatChrome(38) }} [1]</p>
   </td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(iterable)</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{ CompatGeckoDesktop("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{ CompatGeckoDesktop("17") }}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.clear()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("19")}}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.keys(), Set.values(), Set.entries()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("24")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td><code>Set.forEach()</code></td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("25")}}</td>
   <td>{{ CompatIE("11") }}</td>
   <td>25</td>
   <td>7.1</td>
  </tr>
  <tr>
   <td>Value equality for -0 and 0</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoDesktop("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>25</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("41")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(38)}} [1]</td>
   <td>{{ CompatGeckoMobile("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(iterable)</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(38)}}</td>
   <td>{{ CompatGeckoMobile("13") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>iterable</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatGeckoMobile("17") }}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.clear()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("19")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.keys(), Set.values(), Set.entries()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("24")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td><code>Set.forEach()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("25")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>8</td>
  </tr>
  <tr>
   <td>Value equality for -0 and 0</td>
   <td>{{CompatNo}}</td>
   <td>{{ CompatChrome(38) }}</td>
   <td>{{CompatGeckoMobile("29")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Constructor argument: <code>new Set(null)</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Monkey-patched <code>add()</code> in Constructor</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("37")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set[@@species]</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("41")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>Set()</code> without <code>new</code> throws</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("42")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] The feature was available behind a preference from Chrome 31. In <code>chrome://flags</code>, activate the entry “Enable Experimental JavaScript”.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Map")}}</li>
 <li>{{jsxref("WeakMap")}}</li>
 <li>{{jsxref("WeakSet")}}</li>
</ul>
Revert to this revision