mozilla

Revision 363441 of Set

  • Revision slug: JavaScript/Reference/Global_Objects/Set
  • Revision title: Set
  • Revision id: 363441
  • Created:
  • Creator: TarasGlek
  • Is current revision? No
  • Comment

Revision Content

{{ fx_minversion_header("13") }}

{{ Non-standard_header() }}

{{ 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 anytime. 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. Values 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

Method Description
mySet.add(value) Adds the value to mySet. Returns undefined.
mySet.delete(value) Sets the value for the key in mySet. Returns undefined.
mySet.has(value) Returns a boolean asserting whether the value has been added to mySet or not.
mySet.clear() {{ fx_minversion_inline("19") }}Removes all key/value pairs from mySet.
Property Description
mySet.size

Returns the number of key/value pairs in mySet.

{{ fx_minversion_inline("19") }}In Firefox 18 and earlier, 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]

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatNo() }} {{ CompatGeckoDesktop("13") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
iterable {{ CompatNo() }} {{ CompatGeckoMobile("17") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Set.clear() {{ CompatNo() }} {{ CompatGeckoDesktop("19") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatGeckoMobile("13") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
iterable {{ CompatNo() }} {{ CompatGeckoMobile("17") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Set.clear() {{ CompatNo() }} {{ CompatGeckoMobile("19") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Revision Source

<p>{{ fx_minversion_header("13") }}</p>
<p>{{ Non-standard_header() }}</p>
<p>{{ 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 anytime. It is provided as an experimental feature. Do not rely on it for production code.") }}</p>
<p><code>Set</code> objects let you store unique values of any type, whether primitive values or object references. Values equality 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. <code>NaN</code> can also be stored in a <code>Set</code>.</p>
<h2 id="API">API</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th>Method</th>
      <th>Description</th>
    </tr>
    <tr>
      <td><code>mySet.add(value)</code></td>
      <td>Adds the <code>value</code> to <code>mySet</code>. Returns <code>undefined</code>.</td>
    </tr>
    <tr>
      <td><code>mySet</code><code>.delete(</code><code>value</code><code>)</code></td>
      <td>Sets the value for the <code>key</code> in <code>mySet</code>. Returns <code>undefined</code>.</td>
    </tr>
    <tr>
      <td><code>mySet</code><code>.has(</code><code>value</code><code>)</code></td>
      <td>Returns a boolean asserting whether the <code>value</code> has been added to <code>mySet</code> or not.</td>
    </tr>
    <tr>
      <td><code>mySet.clear()</code></td>
      <td>{{ fx_minversion_inline("19") }}Removes all key/value pairs from <code>mySet</code>.</td>
    </tr>
    <tr>
      <th>Property</th>
      <th>Description</th>
    </tr>
    <tr>
      <td><code>mySet.size</code></td>
      <td>
        <p>Returns the number of key/value pairs in <code>mySet</code>.</p>
        {{ fx_minversion_inline("19") }}In Firefox 18 and earlier, <code>size</code> was a method. In Firefox 19 and later it is a property.</td>
    </tr>
  </tbody>
</table>
<h3 id="Examples">Examples</h3>
<pre class="brush: js">
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]

</pre>
<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>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoDesktop("13") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
      <tr>
        <td>iterable</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoMobile("17") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
      <tr>
        <td>Set.clear()</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoDesktop("19") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>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>{{ CompatGeckoMobile("13") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
      <tr>
        <td>iterable</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoMobile("17") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
      <tr>
        <td>Set.clear()</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatGeckoMobile("19") }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=697479">Map and Set bug at Mozilla</a></li>
  <li><a class="external" href="http://wiki.ecmascript.org/doku.php?id=harmony:simple_maps_and_sets">ECMAScript Harmony proposal</a></li>
</ul>
Revert to this revision