MENU
Prototype Methods â Accessors
These accessorfunctions do not modify the original array.Combining & SplittingCombining & Splitting Combining & Splitting :
Array.prototype.concat()
Returns a new array comprised of this array joined with other array(s) and/or value(s).
Array.prototype.slice()
Extracts a section of an array and returns a shallow copy. (Note the difference with splice()).
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = [1,2,3,4,5];
console.log(a.concat(a)); // [1,2,3,4,5,1,2,3,4,5]
console.log(a.slice(2,4)); // [3, 4]
</script></body><html>Searching: Searching: Searching:
Array.prototype.includes()
Determines whether an array contains a certain element, returning true or false as appropriate.
Array.prototype.indexOf()
Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
Array.prototype.lastIndexOf()
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
console.log([NaN].indexOf(NaN)); // -1 failsconsole.log([NaN].includes(NaN)); // true succeeds
</script></body><html>Conversion to String: Conversion to String: Conversion to String:
Array.prototype.join()
Joins all elements of an array into a string.
Array.prototype.toString()
Returns a string representing the array and its elements. Overrides the Object.prototype.toString() method.
Array.prototype.toLocaleString()
Returns a localized string representing the array and its elements. Overrides the Object.prototype.toLocaleString() method.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a = [3];
a=a.concat(4,[5,6]);
console.log(a.join()); // 3,4,5,6
console.log(a.join('-')); // 3-4-5-6
console.log("{"+a+"}"); // {3,4,5,6}
toString() called
console.log(a.slice(1)+""); // 4,5,6
toString() called
console.log(a.slice(2,4).toString()); //5,6, âaâ still not changed
console.log(a.includes([5,6])); // false
console.log(a.includes([5,6],1)); //false,search from index 1
console.log(a.indexOf(4)); // 1
console.log(a.indexOf(4,2)); // -1, search from index 2
console.log([1,[2,3]].lastIndexOf([2,3])); // -1
console.log(typeof [1,[2,3]][1]); // object
console.log(typeof []); // object
a = [1337, new Date(), 'foo'];
console.log(a.toLocaleString('ja-JP',
{ style: 'currency', currency: 'JPY' }));
</script></body><html>Array.prototype.at(index)
Added in ECMAScript 2022 (and shared by String, 11.4, and the TypedArrays, 13), returns the element at index, counting backwards from the last element when index is negative. This is shorter than the classic arr[arr.length-1] idiom, and unlike arr[-1] (which is simply undefined, since arrays are just objects keyed by non-negative-integer-like strings), arr.at(-1) actually works.
var a = [10, 20, 30];
console.log(a.at(0));
console.log(a.at(-1)); // instead of a[a.length - 1]
console.log(a[-1]);Change Array by Copy, added in ECMAScript 2023, gives every mutating method below (7.3) a non-mutating counterpart that returns a new array (or, for .with(), a new array with one element replaced) instead of modifying the receiver in place. Each accepts exactly the same arguments as its mutating counterpart.
| mutates in place | returns a copy instead |
| arr.sort(compareFn) | arr.toSorted(compareFn) |
| arr.reverse() | arr.toReversed() |
| arr.splice(start,deleteCount,...items) | arr.toSpliced(start,deleteCount,...items) |
| arr[index] = value | arr.with(index,value) |
const original = [3, 1, 2];
const sorted = original.toSorted();
console.log(sorted);
console.log(original); // unchanged
const reversed = original.toReversed();
console.log(reversed);
console.log(original.with(0, 'x'));
console.log(original); // still unchangedTypedArrays (13) also gain .at(), .toSorted(), .toReversed(), and .with() (but not .toSpliced(), since typed arrays have a fixed length).