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 1131581 of Array.of()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Array/of
  • Revision title: Array.of()
  • Revision id: 1131581
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{JSRef}}

The Array.of() method creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.

The difference between Array.of() and the Array constructor is in the handling of integer arguments: Array.of(7) creates an array with a single element, 7, whereas Array(7) creates an array with 7 elements, each of which is undefined.

Array.of(7);       // [7] 
Array.of(1, 2, 3); // [1, 2, 3]

Array(7);          // [ , , , , , , ]
Array(1, 2, 3);    // [1, 2, 3]

Syntax

Array.of(element0[, element1[, ...[, elementN]]])

Parameters

elementN
Elements of which to create the array.

Return value

A new {{jsxref("Array")}} instance.

Description

This function is part of the ECMAScript 6 standard. For more information see Array.of and Array.from proposal and Array.of polyfill.

Examples

Array.of(1);         // [1]
Array.of(1, 2, 3);   // [1, 2, 3]
Array.of(undefined); // [undefined]

Polyfill

Running the following code before any other code will create Array.of() if it's not natively available.

if (!Array.of) {
  Array.of = function() {
    return Array.prototype.slice.call(arguments);
  };
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-array.of', 'Array.of')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-array.of', 'Array.of')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(45)}} {{CompatGeckoDesktop("25")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatChrome(39)}} {{CompatGeckoMobile("25")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

See also

  • {{jsxref("Array")}}
  • {{jsxref("Array.from()")}}
  • {{jsxref("TypedArray.of()")}}

Revision Source

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

<p>The <code><strong>Array.of()</strong></code> method creates a new <code>Array</code> instance with a variable number of arguments, regardless of number or type of the arguments.</p>

<p>The difference between <code><strong>Array.of()</strong></code> and the <code><strong>Array</strong></code> constructor is in the handling of integer arguments: <code><strong>Array.of(7)</strong></code> creates an array with a single element, <code>7</code>, whereas <code><strong>Array(7)</strong></code> creates an array with 7 elements, each of which is <code>undefined</code>.</p>

<pre class="brush: js">
Array.of(7);       // [7] 
Array.of(1, 2, 3); // [1, 2, 3]

Array(7);          // [ , , , , , , ]
Array(1, 2, 3);    // [1, 2, 3]
</pre>

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

<pre class="syntaxbox">
Array.of(<var>element0</var>[, <var>element1</var>[, ...[, <var>elementN</var>]]])</pre>

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

<dl>
 <dt><code>element<em>N</em></code></dt>
 <dd>Elements of which to create the array.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A new {{jsxref("Array")}} instance.</p>

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

<p>This function is part of the ECMAScript 6 standard. For more information see <a href="https://gist.github.com/rwaldron/1074126"><code>Array.of</code> and <code>Array.from</code> proposal</a> and <a href="https://gist.github.com/rwaldron/3186576"><code>Array.of</code> polyfill</a>.</p>

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

<pre class="brush: js">
Array.of(1);         // [1]
Array.of(1, 2, 3);   // [1, 2, 3]
Array.of(undefined); // [undefined]
</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>Running the following code before any other code will create <code>Array.of()</code> if it's not natively available.</p>

<pre class="brush: js">
if (!Array.of) {
  Array.of = function() {
    return Array.prototype.slice.call(arguments);
  };
}
</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-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

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

<div>{{CompatibilityTable}}</div>

<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>{{CompatChrome(45)}}</td>
   <td>{{CompatGeckoDesktop("25")}}</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>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(39)}}</td>
   <td>{{CompatGeckoMobile("25")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li>{{jsxref("Array")}}</li>
 <li>{{jsxref("Array.from()")}}</li>
 <li>{{jsxref("TypedArray.of()")}}</li>
</ul>
Revert to this revision