mozilla

Revision 639597 of Set.prototype.add()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Set/add
  • Revision title: Set.prototype.add()
  • Revision id: 639597
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Set")}} {{harmony}}

Summary

The add() method appends a new element with a specified value to the end of a Set object.

Syntax

mySet.add(value);

Parameters

value
Required. The value of the element to add to the Set object.

Return value

The Set object.

Examples

Example: Using the add method

var mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // chainable

console.log(mySet);
// Set [1, 5, "some text"]

Specifications

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

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 31 {{CompatGeckoDesktop("13.0")}} 11 {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} {{ CompatNo() }} {{CompatGeckoMobile("13.0")}} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

Chrome-specific notes

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

Firefox-specific notes

  • Prior to Firefox 33 {{geckoRelease("33")}}, Set.prototype.add returned undefined and was not chainable. This has been fixed ({{bug(1031632)}}). The behavior can be found in Chrome/v8 as well (issue).

See also

  • {{jsxref("Set")}}
  • {{jsxref("Set.prototype.delete()")}}
  • {{jsxref("Set.prototype.has()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Set")}} {{harmony}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>add()</strong></code> method appends a new element with a specified&nbsp;<code>value</code> to the end of a <code>Set</code> object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>mySet</em>.add(value);</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
 <dt>
  value</dt>
 <dd>
  Required. The value of the element to add to the <code>Set</code> object.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>The <code>Set</code> object.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">Example: Using the <code>add</code> method</h3>
<pre class="brush: js">
var mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // chainable

console.log(mySet);
// Set [1, 5, "some text"]
</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.prototype.add', 'Set.prototype.add')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</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>31</td>
    <td>{{CompatGeckoDesktop("13.0")}}</td>
    <td>11</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>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>{{ CompatNo() }}</td>
    <td>{{CompatGeckoMobile("13.0")}}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h3 id="Chrome-specific_notes">Chrome-specific notes</h3>
<ul>
 <li>The feature is available behind a preference. In <code>chrome://flags</code>, activate the entry “Enable Experimental JavaScript”.</li>
</ul>
<h3 id="Firefox-specific_notes">Firefox-specific notes</h3>
<ul>
 <li>Prior to Firefox 33 {{geckoRelease("33")}}, <code>Set.prototype.add</code> returned <code>undefined</code> and was not chainable. This has been fixed ({{bug(1031632)}}). The behavior can be found in Chrome/v8 as well (<a href="https://code.google.com/p/v8/issues/detail?id=3410">issue</a>).</li>
</ul>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Set")}}</li>
 <li>{{jsxref("Set.prototype.delete()")}}</li>
 <li>{{jsxref("Set.prototype.has()")}}</li>
</ul>
Revert to this revision