Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array toSpliced Method

JavaScript Array toSpliced() Method

Array Object Reference JavaScript Array object

Example

Reverses the order of elements in an array:

//

Creating an Array

const

sites

=

[

"

Google

"

,

"

Runoob

"

,

"

JYSHARE

"

,

"

Taobao

"

]

;

//

Add "Zhihu" and "Wiki" at position 2

const

sites2

=

sites

.

toSpliced

(

2

,

0

,

"

Zhihu

"

,

"

Wiki

"

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

sites2

;

Output result:

Google,Runoob,Zhihu,Wiki,JYSHARE,Taobao

Try it »


Definition and Usage

The toSpliced() method is used to add and/or remove elements from an array.

The toSpliced() method returns a new array.

The toSpliced() method does not modify the original array.

toSpliced() is a copy version of the splice() method.


Browser support

toSpliced() is a feature of ES2023.

Since July 2023, all modern browsers support this feature:

Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
Feb 2023 Feb 2023 Jul 2023 Mar 2023 May 2023

Syntax

array.toSpliced(index, count, item1, ..., itemX)

Parameters

Parameters Description
index (required) The index (position) at which to add or remove elements. Negative values count from the end of the array.
count (optional) The number of elements to remove.
item1, ... (optional) The new elements to add.

Return Value

Type Description
Array A new array containing the modifications.

More examples

Remove elements:

Example

const

fruits

=

[

"Banana"

,

"Orange"

,

"Apple"

,

"Mango"

]

;

const

fruits2

=

fruits.

toSpliced

(

2

,

2

)

;

// ["Banana", "Orange"]

Remove and add elements:

Example

const

fruits

=

[

"Banana"

,

"Orange"

,

"Apple"

,

"Mango"

]

;

const

fruits2

=

fruits.

toSpliced

(

2

,

1

,

"Lemon"

,

"Kiwi"

)

;

// ["Banana", "Orange", "Lemon", "Kiwi", "Mango"]


Array Object Reference JavaScript Array object