toString()
The toString()
method of Array
instances returns a string representing the specified array and its elements.
The Array
object overrides the toString
method of Object
. The toString
method of arrays calls join()
internally, which joins the array and returns one string containing each array element separated by commas. If the join
method is unavailable or is not a function, Object.prototype.toString
is used instead, returning [object Array]
.
JavaScript calls the toString
method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation. Array.prototype.toString
recursively converts each element, including other arrays, to strings. Because the string returned by Array.prototype.toString
does not have delimiters, nested arrays look like they are flattened.
When an array is cyclic (it contains an element that is itself), browsers avoid infinite recursion by ignoring the cyclic reference.
join()
The join()
method of Array
instances creates and returns a new string by concatenating all of the elements in this array, separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
delete()
concat()
The concat()
method of Array
instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
concat
does not treat all array-like objects as arrays by default — only if Symbol.isConcatSpreadable
is set to a truthy value (e.g. true
).
If any of the source arrays is sparse, the resulting array will also be sparse:
If the this
value is not an array, it is converted to an object and then treated in the same way as the arguments for concat()
. In this case the return value is always a plain new array.
sort()
The sort()
method of Array
instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. The time and space complexity of the sort cannot be guaranteed as it depends on the implementation. To sort the elements in an array without mutating the original array, use toSorted()
.
toSorted()
The toSorted()
method of Array
instances is the copying version of the sort()
method. It returns a new array with the elements sorted in ascending order. When used on sparse arrays, the toSorted()
method iterates empty slots as if they have the value undefined
. The toSorted()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.
Empty slots are sorted as if they have the value undefined
. They are always sorted to the end of the array and compareFn
is not called for them.
The toSorted()
method reads the length
property of this
. It then collects all existing integer-keyed properties in the range of 0
to length - 1
, sorts them, and writes them into a new array.
reverse()
The reverse()
method of Array
instances reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated. To reverse the elements in an array without mutating the original array, use toReversed()
.
The reverse()
method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.
The reverse()
method preserves empty slots. If the source array is sparse, the empty slots' corresponding new indices are deleted and also become empty slots.
The reverse()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Reversing the elements in an array
The following example creates an array items
, containing three elements, then reverses the array. The call to reverse()
returns a reference to the reversed array items
.
The reverse() method returns the reference to the same array
The reverse()
method returns reference to the original array, so mutating the returned array will mutate the original array as well.
In case you want reverse()
to not mutate the original array, but return a shallow-copied array like other array methods (e.g. map()
) do, use the toReversed()
method. Alternatively, you can do a shallow copy before calling reverse()
, using the spread syntax or Array.from()
.
Using reverse() on sparse arrays
Sparse arrays remain sparse after calling reverse()
. Empty slots are copied over to their respective new indices as empty slots.
Calling reverse() on non-array objects
The reverse()
method reads the length
property of this
. It then visits each property having an integer key between 0
and length / 2
, and swaps the two corresponding indices on both ends, deleting any destination property for which the source property did not exist.
toReversed()
The toReversed()
method of Array
instances is the copying counterpart of the reverse()
method. It returns a new array with the elements in reversed order.
The following example creates an array items
, containing three elements, then creates a new array that's the reverse of items
. The items
array remains unchanged.
The return value of toReversed()
is never sparse. Empty slots become undefined
in the returned array.
The toReversed()
method reads the length
property of this
. It then visits each property having an integer key between length - 1
and 0
in descending order, adding the value of the current property to the end of the array to be returned.
splice()
The splice()
method of Array
instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced()
. To access part of an array without modifying it, see slice()
.
The splice()
method is a mutating method. It may change the content of this
. If the specified number of elements to insert differs from the number of elements being removed, the array's length
will be changed as well. At the same time, it uses @@species
to create a new array instance to be returned.
If the deleted portion is sparse, the array returned by splice()
is sparse as well, with those corresponding indices being empty slots.
The splice()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
The splice()
method preserves the array's sparseness.
The splice()
method reads the length
property of this
. It then updates the integer-keyed properties and the length
property as needed.
toSpliced()
The toSpliced()
method of Array
instances is the copying version of the splice()
method. It returns a new array with some elements removed and/or replaced at a given index.
The toSpliced()
method never produces a sparse array. If the source array is sparse, the empty slots will be replaced with undefined
in the new array. The toSpliced()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.
You can use toSpliced()
to delete, add, and replace elements in an array and create a new array more efficiently than using slice()
and concat()
.
The toSpliced()
method always creates a dense array.
The toSpliced()
method reads the length
property of this
. It then reads the integer-keyed properties needed and writes them into the new array.
slice()
The slice()
method of Array
instances returns a shallow copy of a portion of an array into a new array object selected from start
to end
(end
not included) where start
and end
represent the index of items in that array. The original array will not be modified.
The slice()
method is a copying method. It does not alter this
but instead returns a shallow copy that contains some of the same elements as the ones from the original array.
The slice()
method preserves empty slots. If the sliced portion is sparse, the returned array is sparse as well.
The slice()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.
In the following example, slice
creates a new array, newCar
, from myCar
. Both include a reference to the object myHonda
. When the color of myHonda
is changed to purple, both arrays reflect the change.
The slice()
method reads the length
property of this
. It then reads the integer-keyed properties from start
to end
and defines them on a newly created array.
The slice()
method is often used with bind()
and call()
to create a utility method that converts an array-like object into an array.
The array returned from slice()
may be sparse if the source is sparse.