Course recommendation:

JavaScript Array splice()

splice() Definition and usage

The method adds/removes items to/from the array and returns the removed items.splice() Note:

The method changes the original array.

Instance

Example 1

var fruits = ["Banana", "Orange", "Apple", "Mango"];
Add items to the array:

Try it yourself

Example 2

Add new items and remove 1 item at position 2:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");

Try it yourself

Example 3

Remove 2 items at position 2:

var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);

Try it yourself

Syntax

array.splice(index, howmany, item1, ....., itemX)

Parameter value

Parameter Description
index Required. An integer, specifying at what position to add/remove items, using negative values to specify positions from the end of the array.
howmany Optional. The number of items to be removed. If set to 0, no items will be removed.
item1, ..., itemX Optional. The new item(s) to be added to the array.

Technical details

Return value: A new array containing the removed items (if any).
JavaScript version: ECMAScript 1

Browser support

All browsers fully support splice() Method:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

Tutorial:JavaScript Array

Tutorial:JavaScript Array Const

Tutorial:JavaScript Array Methods

Tutorial:JavaScript Sorting Array

Tutorial:JavaScript Array Iteration