JavaScript Array toSpliced()
- Previous page toSorted()
- Next page toString()
- Go up one level JavaScript Array Reference Manual
Definition and Usage
toSpliced()
The method is used to add and/or remove array elements.
toSpliced()
The method returns a new array.
toSpliced()
The method does not change the original array.
toSpliced()
The method is splice()
The copy version of the method.
See also:
Example
Example 1
// Create an array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Add "Lemon" and "Kiwi" at position 2 const fruits2 = fruits.toSpliced(2, 0, "Lemon", "Kiwi");
Example 2
// Create an array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Remove 2 elements at position 2 const fruits2 = fruits.toSpliced(2, 2);
Example 3
// Create an array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Remove 1 element at position 2 and add "Lemon" and "Kiwi" const fruits2 = fruits.toSpliced(2, 1, "Lemon", "Kiwi");
Syntax
array.toSpliced(index, count, item1, ....., itemX)
Parameter
Parameter | Description |
---|---|
index |
Required. The index (position) of the elements to be added or removed. Negative values start from the end of the array. |
count | Optional. The number of elements to be removed. |
item1,... | Optional. The new elements to be added. |
Return Value
Type | Description |
---|---|
Array | New array with changes included. |
Browser Support
toSpliced()
It is a feature of ES2023.
Starting from July 2023, all modern browsers support this method:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 110 | Edge 110 | Firefox 115 | Safari 16.4 | Opera 96 |
February 2023 | February 2023 | July 2023 | March 2023 | May 2023 |
- Previous page toSorted()
- Next page toString()
- Go up one level JavaScript Array Reference Manual