The Array.prototype
property represents the prototype for the Array
constructor and allows you to add new properties and methods to all Array
objects.
// If JavaScript doesn't provide a first() method natively, // add a new method returning the first element of an array. if (!Array.prototype.first) { Array.prototype.first = function() { return this[0]; } }
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. For example, you can add new methods and properties to extend all Array
objects. This is used for polyfilling, for example.
Little known fact: Array.prototype
itself is an Array
:
Array.isArray(Array.prototype); // true
Property attributes of Array.prototype
| |
---|---|
Writable | no |
Enumerable | no |
Configurable | no |
Array.prototype.constructor
Array.prototype.length
Array.prototype[@@unscopables]
with
binding scope.These methods modify the array:
Array.prototype.copyWithin()
Array.prototype.fill()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.unshift()
These methods do not modify the array and return some representation of the array.
Array.prototype.concat()
Array.prototype.includes()
true
or false
as appropriate.Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.lastIndexOf()
Array.prototype.slice()
Array.prototype.toSource()
Object.prototype.toSource()
method.Array.prototype.toString()
Object.prototype.toString()
method.Array.prototype.toLocaleString()
Object.prototype.toLocaleString()
method.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.
Array.prototype.entries()
Array Iterator
object that contains the key/value pairs for each index in the array.Array.prototype.every()
Array.prototype.filter()
Array.prototype.find()
undefined
if not found.Array.prototype.findIndex()
Array.prototype.forEach()
Array.prototype.keys()
Array Iterator
that contains the keys for each index in the array.Array.prototype.map()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.some()
Array.prototype.values()
Array Iterator
object that contains the values for each index in the array.Array.prototype[@@iterator]()
Array Iterator
object that contains the values for each index in the array.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). 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.
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype' in that specification. | Standard | Added the copyWithin() , fill() , entries() , keys() , values() , find() , findIndex() methods. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Array.prototype' in that specification. | Draft | Added the includes() method. |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype