English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Array splice() Method

 JavaScript Array Object

splice()The method changes the array by removing existing elements or adding new ones.

If you specify a different number of elements to insert than to delete, the length of the array will also be different.

Note: The splice() method will change the original array.

Syntax:

array.splice(start, deleteCount, [item1, item2, ...])
var months = [&39;Jan', &39;Mar', &39;Apr', &39;Jun'];
months.splice(1, 0, 'Feb');// adding 'Feb'May&# 1
Test and see‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the splice() method:

Method
splice()11isis5.5

Parameter value

ParameterDescription
startStart adding in the array/The index of the element to be removed. Negative values specify positions from the end of the array.
deleteCount(Optional) The number of elements to be removed. If set to 0, no elements will be removed
item1, item2, ...(Optional) Elements to be added to the array, starting fromstartingindexstartIf no elements are specified, splice() will only remove elements from the array.

技术细节

Technical DetailsReturn value:
An array containing the deleted elements (if any)JavaScript Version: 1

ECMAScript

More examples4in the1elements:

var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun'];
months.splice(4, 1, &39; with &39;);   // replacing at index39;Jun'replace &39; with &39;May&# 4
Test and see‹/›

; at index3from index1elements:

var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun'];
months.splice(3, 1);
Test and see‹/›

; at index2from index2elements:

var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun'];
months.splice(2, 2);
Test and see‹/›

by removing from index 02elements, and insert4values:

var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun'];
months.splice(0, 2, &39;A', &39;B', &39;C', &39;D');
Test and see‹/›

Thesplice()The method returns an array containing the deleted elements:

var months = [&39;Jan', &39;Feb', &39;Mar', &39;Apr', &39;Jun'];
var arr = months.splice(2, 2);
Test and see‹/›

 JavaScript Array Object