Array.prototype

  • Revision slug: JavaScript/Reference/Global_Objects/Array/prototype
  • Revision title: Array.prototype
  • Revision id: 371127
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment

Revision Content

Summary

Represents the prototype for the Array constructor.

Description

Array instances inherit from Array.prototype. As with all constructors, you can change the constructor's prototype object to make changes to all Array instances.

Properties

constructor
Specifies the function that creates an object's prototype.
length
Reflects the number of elements in an array.
{{jsOverrides("Object", "Methods", "constructor", "index", "input", "length")}}

Methods

Mutator methods

These methods modify the array:

pop
Removes the last element from an array and returns that element.
push
Adds one or more elements to the end of an array and returns the new length of the array.
reverse
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
shift
Removes the first element from an array and returns that element.
sort
Sorts the elements of an array in place and returns the array.
splice
Adds and/or removes elements from an array.
unshift
Adds one or more elements to the front of an array and returns the new length of the array.

Accessor methods

These methods do not modify the array and return some representation of the array.

concat
Returns a new array comprised of this array joined with other array(s) and/or value(s).
join
Joins all elements of an array into a string.
slice
Extracts a section of an array and returns a new array.
toSource {{Non-standard_inline()}}
Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.prototype.toSource method.
toString
Returns a string representing the array and its elements. Overrides the Object.prototype.toString method.
indexOf {{js_minversion_inline(1.6)}}
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
lastIndexOf {{js_minversion_inline(1.6)}}
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

Iteration methods

Several methods take as arguments functions to be called back while processing the array. When these methods are called, the length of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. While the specific behavior of these methods in such cases is well-defined, you should not rely upon it so as not to confuse others who might read your code. If you must mutate the array, copy into a new array instead.

forEach {{js_minversion_inline(1.6)}}
Calls a function for each element in the array.
every {{js_minversion_inline(1.6)}}
Returns true if every element in this array satisfies the provided testing function.
some {{js_minversion_inline(1.6)}}
Returns true if at least one element in this array satisfies the provided testing function.
filter {{js_minversion_inline(1.6)}}
Creates a new array with all of the elements of this array for which the provided filtering function returns true.
map {{js_minversion_inline(1.6)}}
Creates a new array with the results of calling a provided function on every element in this array.
reduce {{js_minversion_inline("1.8")}}
Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight {{js_minversion_inline("1.8")}}
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

Generic methods

Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a length property, and which can usefully be accessed using numeric property names (as with array[5] indexing). TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like JavaArray or String. Some methods, such as join, only read the length and numeric properties of the object they are called on. Others, like reverse, require that the object's numeric properties and length be mutable; these methods can therefore not be called on objects like String, which does not permit its length property or synthesized numeric properties to be set.

{{Js_minversion_header(1.6)}}
{{jsOverrides("Object", "Methods", "pop", "push", "reverse", "shift", "sort", "splice", "unshift", "concat", "join", "slice", "toSource", "toString", "indexOf", "lastIndexOf", "filter", "forEach", "every", "map", "some", "reduce", "reduceRight")}}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Represents the prototype for the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global_Objects/Array">Array</a> constructor.</p>
<h2 id="Description" name="Description">Description</h2>
<p><a class="internal" href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global Objects/Array"><code>Array</code></a> instances inherit from <code>Array.prototype</code>. As with all constructors, you can change the constructor's prototype object to make changes to all <code>Array</code> instances.</p>
<h2 id="Properties">Properties</h2>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/constructor" title="JavaScript/Reference/Global_Objects/Array/constructor">constructor</a></dt>
  <dd>
    Specifies the function that creates an object's prototype.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/length" title="JavaScript/Reference/Global_Objects/Array/length">length</a></dt>
  <dd>
    Reflects the number of elements in an array.</dd>
</dl>
<div>
  {{jsOverrides("Object", "Methods", "constructor", "index", "input", "length")}}</div>
<h2 id="Methods">Methods</h2>
<h3 id="Mutator_methods">Mutator methods</h3>
<p>These methods modify the array:</p>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/pop" title="JavaScript/Reference/Global_Objects/Array/pop">pop</a></dt>
  <dd>
    Removes the last element from an array and returns that element.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="JavaScript/Reference/Global_Objects/Array/push">push</a></dt>
  <dd>
    Adds one or more elements to the end of an array and returns the new length of the array.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/reverse" title="JavaScript/Reference/Global_Objects/Array/reverse">reverse</a></dt>
  <dd>
    Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/shift" title="JavaScript/Reference/Global_Objects/Array/shift">shift</a></dt>
  <dd>
    Removes the first element from an array and returns that element.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort" title="JavaScript/Reference/Global_Objects/Array/sort">sort</a></dt>
  <dd>
    Sorts the elements of an array in place and returns the array.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice" title="JavaScript/Reference/Global_Objects/Array/splice">splice</a></dt>
  <dd>
    Adds and/or removes elements from an array.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/unshift" title="JavaScript/Reference/Global_Objects/Array/unshift">unshift</a></dt>
  <dd>
    Adds one or more elements to the front of an array and returns the new length of the array.</dd>
</dl>
<h3 id="Accessor_methods">Accessor methods</h3>
<!-- need a better name... -->
<p>These methods do not modify the array and return some representation of the array.</p>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat" title="JavaScript/Reference/Global_Objects/Array/concat">concat</a></dt>
  <dd>
    Returns a new array comprised of this array joined with other array(s) and/or value(s).</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/join" title="JavaScript/Reference/Global_Objects/Array/join">join</a></dt>
  <dd>
    Joins all elements of an array into a string.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice" title="JavaScript/Reference/Global_Objects/Array/slice">slice</a></dt>
  <dd>
    Extracts a section of an array and returns a new array.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/toSource" title="JavaScript/Reference/Global_Objects/Array/toSource">toSource</a> {{Non-standard_inline()}}</dt>
  <dd>
    Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toSource" title="JavaScript/Reference/Global_Objects/Object/toSource">Object.prototype.toSource</a> method.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/toString" title="JavaScript/Reference/Global_Objects/Array/toString">toString</a></dt>
  <dd>
    Returns a string representing the array and its elements. Overrides the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString">Object.prototype.toString</a> method.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf" title="JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/lastIndexOf" title="JavaScript/Reference/Global_Objects/Array/lastIndexOf">lastIndexOf</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.</dd>
</dl>
<h3 id="Iteration_methods">Iteration methods</h3>
<p>Several methods take as arguments functions to be called back while processing the array. When these methods are called, the <code>length</code> of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. While the specific behavior of these methods in such cases is well-defined, you should not rely upon it so as not to confuse others who might read your code. If you must mutate the array, copy into a new array instead.</p>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach" title="JavaScript/Reference/Global_Objects/Array/forEach">forEach</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Calls a function for each element in the array.</dd>
</dl>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/every" title="JavaScript/Reference/Global_Objects/Array/every">every</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Returns true if every element in this array satisfies the provided testing function.</dd>
</dl>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/some" title="JavaScript/Reference/Global_Objects/Array/some">some</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Returns true if at least one element in this array satisfies the provided testing function.</dd>
</dl>
<dl>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter" title="JavaScript/Reference/Global_Objects/Array/filter">filter</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Creates a new array with all of the elements of this array for which the provided filtering function returns true.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/map" title="JavaScript/Reference/Global_Objects/Array/map">map</a> {{js_minversion_inline(1.6)}}</dt>
  <dd>
    Creates a new array with the results of calling a provided function on every element in this array.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce" title="JavaScript/Reference/Objects/Array/reduce">reduce</a> {{js_minversion_inline("1.8")}}</dt>
  <dd>
    Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.</dd>
  <dt>
    <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/ReduceRight" title="JavaScript/Reference/Objects/Array/reduceRight">reduceRight</a> {{js_minversion_inline("1.8")}}</dt>
  <dd>
    Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.</dd>
</dl>
<h3 id="Generic_methods">Generic methods</h3>
<p>Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a <code>length</code> property, and which can usefully be accessed using numeric property names (as with <code>array[5]</code> indexing). <span class="comment">TODO: give examples with Array.prototype.forEach.call, and adding the method to an object like <a href="/en-US/docs/JavaScript/Reference/Global_Objects/JavaArray" title="JavaScript/Reference/Global_Objects/JavaArray">JavaArray</a> or <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String">String</a>.</span> Some methods, such as <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/join" title="JavaScript/Reference/Global_Objects/Array/join">join</a></code>, only read the <code>length</code> and numeric properties of the object they are called on. Others, like <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/reverse" title="JavaScript/Reference/Global_Objects/Array/reverse">reverse</a></code>, require that the object's numeric properties and <code>length</code> be mutable; these methods can therefore not be called on objects like <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String">S</a><code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String">tring</a></code>, which does not permit its <code>length</code> property or synthesized numeric properties to be set. <!--The (superfluous) note about which methods modify the array has been removed, as that distinction is already made above.--></p>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat" title="JavaScript/Reference/Global_Objects/Array/concat">concat</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/join" title="JavaScript/Reference/Global_Objects/Array/join">join</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/pop" title="JavaScript/Reference/Global_Objects/Array/pop">pop</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="JavaScript/Reference/Global_Objects/Array/push">push</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/reverse" title="JavaScript/Reference/Global_Objects/Array/reverse">reverse</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/shift" title="JavaScript/Reference/Global_Objects/Array/shift">shift</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice" title="JavaScript/Reference/Global_Objects/Array/slice">slice</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort" title="JavaScript/Reference/Global_Objects/Array/sort">sort</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice" title="JavaScript/Reference/Global_Objects/Array/splice">splice</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/toSource" title="JavaScript/Reference/Global_Objects/Array/toSource">toSource</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/toString" title="JavaScript/Reference/Global_Objects/Array/toString">toString</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/unshift" title="JavaScript/Reference/Global_Objects/Array/unshift">unshift</a></li>
</ul>
<div>
  {{Js_minversion_header(1.6)}}</div>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/every" title="JavaScript/Reference/Global_Objects/Array/every">every</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter" title="JavaScript/Reference/Global_Objects/Array/filter">filter</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach" title="JavaScript/Reference/Global_Objects/Array/forEach">forEach</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf" title="JavaScript/Reference/Global_Objects/Array/indexOf">indexOf</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/lastIndexOf" title="JavaScript/Reference/Global_Objects/Array/lastIndexOf">lastIndexOf</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/map" title="JavaScript/Reference/Global_Objects/Array/map">map</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/some" title="JavaScript/Reference/Global_Objects/Array/some">some</a></li>
</ul>
<div>
  {{jsOverrides("Object", "Methods", "pop", "push", "reverse", "shift", "sort", "splice", "unshift", "concat", "join", "slice", "toSource", "toString", "indexOf", "lastIndexOf", "filter", "forEach", "every", "map", "some", "reduce", "reduceRight")}}</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="JavaScript/Reference/Global_Objects/Array">JavaScript Reference:Global Objects:Array</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/prototype" title="JavaScript/Reference/Global_Objects/Function/prototype">Function.prototype</a></li>
</ul>
Revert to this revision