JavaScript Array toSpliced()
Examples
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, add "Lemon" and "Kiwi":
const fruits2 = fruits.toSpliced(2, 0, "Lemon", "Kiwi");
Try it Yourself »
More Examples Below !
Description
The toSpliced() method adds and/or removes array elements.
The toSpliced() method returns a new array.
The toSpliced() method does not change the original array.
The toSpliced() method is the copying version of the
splice() method.
Syntax
array.toSpliced(index, count, item1, ....., itemX)
Parameters
| Parameter | Description |
| index | Required. The index (position) to add or remove items. A negative value counts from the end of the array. |
| count | Optional. Number of items to be removed. |
| item1,... | Optional. The new elements(s) to be added. |
Return Value
| Type | Description |
| Array | A new array including the changes. |
More Examples
Example
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, remove 2 items
const fruits2 = fruits.toSpliced(2, 2);
Try it Yourself »
Example
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// At position 2, remove 1 item, add "Lemon" and "Kiwi"
const fruits2 = fruits.toSpliced(2, 1, "Lemon", "Kiwi");
Try it Yourself »
Array Tutorials:
Browser Support
toSpliced() is a JavaScript 2023 feature.
ES 2023 is supported in all modern browsers since July 2023:
| Chrome 110 |
Edge 110 |
Firefox 115 |
Safari 16.4 |
Opera 96 |
| Feb 2023 | Feb 2023 | Jul 2023 | Mar 2023 | May 2023 |