Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 870 of Array.isArray()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/isArray
  • Revision title: isArray
  • Revision id: 870
  • Created:
  • Creator: chernikov
  • Is current revision? No
  • Comment 6 words added, 3 words removed

Revision Content

{{ js_minversion_header("1.8.5") }}

Summary

Returns true if an object is an array, false if it is not.

Method of Object
Implemented in JavaScript 1.8.5
ECMAScript Edition ECMAScript 5th Edition

Syntax

Array.isArray(obj)

Parameters

obj
The object to be checked

Description

This function is part of the ECMAScript 5 standard. See the Web Tech Blog for more details.

Examples

// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray( new Array() );
Array.isArray( Array.prototype ); // Little known fact: Array.prototype is itself an array.

// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray("Array");
Array.isArray(true);
Array.isArray(false);

Compatiblity

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

if(!Array.isArray) {
  Array.isArray = (function() {
    var builtInToString = Object.prototype.toString; // save a reference built-in Object.prototype.toString
    var builtInToCall = Function.prototype.call; // save a reference to built-in Function.prototype.call
    var callWithArgs = builtInToCall.bind(builtInToCall); // requires a built-in bind function, not a shim
 
    var argToString = function(o) {
      return callWithArgs(builtInToString, o);
    };
                     
    return function(o) { 
      return argToString(o) === '[object Array]';
    };
  })();
}

Cross-browser compatibility

Based on Kangax's compat table.

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4 (2.0) 5 9 {{ CompatNo() }} 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<h2 name="Summary">Summary</h2>
<p>Returns <code>true</code> if an object is an array, <code>false</code> if it is not.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.8.5</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h2 name="Syntax">Syntax</h2>
<p><code>Array.isArray(<em>obj</em>)</code></p>
<h2 name="Parameters">Parameters</h2>
<dl> <dt>obj</dt> <dd>The object to be checked</dd>
</dl>
<h2 name="Description">Description</h2>
<p>This function is part of the ECMAScript 5 standard. See the <a href="/web-tech/2010/07/26/determining-with-absolute-accuracy-whether-or-not-a-javascript-object-is-an-array" title="web-tech/2010/07/26/determining-with-absolute-accuracy-whether-or-not-a-javascript-object-is-an-array">Web Tech Blog</a> for more details.</p>
<h2>Examples</h2>
<pre class="brush: js">// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray( new Array() );
Array.isArray( Array.prototype ); // Little known fact: Array.prototype is itself an array.

// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray("Array");
Array.isArray(true);
Array.isArray(false);</pre>
<h2 name="See_also">Compatiblity</h2>
<p>Running the following code before any other code will create <code>Array.isArray</code> if it's not natively available.</p>
<pre class="brush: js">if(!Array.isArray) {
  Array.isArray = (function() {
    var builtInToString = Object.prototype.toString; // save a reference built-in Object.prototype.toString
    var builtInToCall = Function.prototype.call; // save a reference to built-in Function.prototype.call
    var callWithArgs = builtInToCall.bind(builtInToCall); // requires a built-in bind function, not a shim
 
    var argToString = function(o) {
      return callWithArgs(builtInToString, o);
    };
                     
    return function(o) { 
      return argToString(o) === '[object Array]';
    };
  })();
}
</pre>
<h2>Cross-browser compatibility</h2>
<p>Based on <a class=" external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>4 (2.0)</td> <td>5</td> <td>9</td> <td>{{ CompatNo() }}</td> <td>5</td> </tr> </tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody>
</table>
</div>
<h2 name="See_also">See also</h2>
<ul> <li><a class=" link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=510537">Bug 510537</a> -<span id="summary_alias_container"> <span id="short_desc_nonedit_display">ES5: Array.isArray</span> </span></li>
</ul>
Revert to this revision