At position 2, add the new items, and remove 1 item: - Javascript Array

Javascript examples for Array:splice

Description

At position 2, add the new items, and remove 1 item:

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"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {/*  w w w.  j av  a 2s. c  o  m*/
    fruits.splice(2, 1, "Lemon", "Kiwi");
    document.getElementById("demo").innerHTML = fruits;
}
</script>

</body>
</html>

Related Tutorials