Array splice() Method - Javascript Array

Javascript examples for Array:splice

Description

The splice() method adds/removes items, and returns the removed item(s).

This method changes the original array.

Syntax

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

Parameter Values

Parameter Description
index Required. An integer that specifies at what position to add/remove items, negative values starts from the end of the array
count 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

Return Value:

A new Array, containing the removed items (if any)

remove two elements from the array

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
var fruits = ["a","b","c","d","e", "Kiwi"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {//w  w  w  . java 2s.c om
    fruits.splice(2, 2);
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

Related Tutorials