JS: Array.prototype
What is Array.prototype
Purpose
Purpose of Array.prototype is to provide methods and properties useful for all array objects
Array.prototype is the parent of all array objects.
Type
interesting note
The Array prototype object is an Array exotic object …
Note: The Array prototype object is specified to be an Array exotic object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification. Pre-ES2015 code (and some engines) treated Array.prototype as a real array. Making it a plain object would have broken existing programs, so the committee kept the exotic-array behavior.
Type of Array.prototype is Object
.
console.assert(typeof Array.prototype === "object");
Array.prototype is a true array. Meaning, you can push elements to it, and with a magical length property.
console.assert((Array.isArray(Array.prototype)) === true); console.assert(Array.prototype.length === 0); Array.prototype.push("x"); console.assert(Array.prototype[0] === "x"); console.assert(Array.prototype.length === 1);
Properties
Value Properties
Function Properties
Get Set an element by index
Add, Remove
- JS: Array.prototype.pop
- JS: Array.prototype.push
- JS: Array.prototype.shift
- JS: Array.prototype.unshift
Add or Remove Subarray
- JS: Array.prototype.concat
- JS: Array.prototype.slice (extract)
- JS: Array.prototype.splice (remove and add at index)
- JS: Array.prototype.toSpliced (λ)
Sort, Reverse
- JS: Array.prototype.sort
- JS: Array.prototype.toSorted
- JS: Array.prototype.reverse
- JS: Array.prototype.toReversed
Map, Iteration, Reduce
- JS: Array.prototype.map
- JS: Array.prototype.flatMap
- JS: Array.prototype.forEach
- JS: Array.prototype.filter
- JS: Array.prototype.fill
- JS: Array.prototype.copyWithin
- JS: Array.prototype.some
- JS: Array.prototype.every
- JS: Array.prototype.reduce
- JS: Array.prototype.reduceRight
- JS: Array.prototype.flat
Find element
Convert to string
Array Iterator
Other
Array.prototype.toLocaleStringArray.prototype[Symbol.iterator](same as Array.prototype.values)Array.prototype[Symbol.unscopables]