Easy Tutorial
❮ Prop Style Transformorigin Prop Style Border ❯

JavaScript splice() Method

JavaScript Array Object

Example

Adding new elements to an array:

fruits output result:


Definition and Usage

The splice() method is used to add or remove elements from an array.

Note: This method modifies the original array.

Return Value

If an element is removed, it returns an array with the removed element. If no elements are removed, it returns an empty array.


Browser Support

All major browsers support the splice() method.

Chrome IE Edge Firefox Safari Opera
Supported Supported Supported Supported Supported Supported

Syntax

Parameter Values

Parameter Description
index Required. Specifies where to add/remove elements. <br>This parameter is the index at which to start inserting and/or deleting array elements, and must be a number.
howmany Optional. Specifies how many elements should be removed. It must be a number, but can be "0". <br>If this parameter is not provided, all elements from the index to the end of the array will be removed.
item1, ..., itemX Optional. New elements to be added to the array

Return Value

Type Description
Array Returns an array containing the deleted elements from arrayObject.

Technical Details

| JavaScript Version: | 1.2 | | --- | --- |


More Examples

Example

Removing the third element from the array and adding new elements at the third position:

fruits output result:

Banana,Orange,Lemon,Kiwi,Mango

Example

Deleting two elements starting from the third position in the array:

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

fruits output result:

Banana,Orange

Example: Removing a Specific Element from the Array

const array = [2, 5, 9];

document.write(array);

const index = array.indexOf(5); // Removing element 5
if (index > -1) { // Removing the found element
  array.splice(index, 1); // Removing the element
}
document.write("<br>")
// array = [2, 9]
document.write(array);

JavaScript Array Object

❮ Prop Style Transformorigin Prop Style Border ❯