Splice an Array

YOU ARE HERE

The splice() method can be used to add new items to an array. The first parameter (2) defines the position where new elements should be added (spliced in). The second parameter (0) defines how many elements should be removed. The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
HTML file: Displayed by browser:
<body>
<h4>Original array:</h4>
<p id="demo"></p>
<hr />
<h4>Status of deleted index in the array:</h4>
<button onclick="myFunction()">Try it</button>
<p id="demo1"></p>
<hr />
<h4>Current elements in the array after deletion:</h4>
<button onclick="myElements()">New Array</button>
<p id="demo2"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
    delete fruits[0];    // Delete first element of the array
    document.getElementById("demo1").innerHTML = fruits[0];
}
function myElements() {
    document.getElementById("demo2").innerHTML = fruits;
}
</script>
</body>
MODIFY THE HTML CODING
Under Construction

TEXT

BackTable of ContentsNext