Copyright © tutorialspoint.com

Javascript Array splice() Method

previous next

Advertisements


Description:

Javascript array splice() method changes the content of an array, adding new elements while removing old elements.

Syntax:

array.splice(index, howMany, [element1][, ..., elementN]);

Here is the detail of parameters:

Return Value:

Returns the extracted array based on the passed parameters.

Example:

<html>
<head>
<title>JavaScript Array splice Method</title>
</head>
<body>
<script type="text/javascript">
var arr = ["orange", "mango", "banana", "sugar", "tea"];

var removed = arr.splice(2, 0, "water");
document.write("After adding 1: " + arr );
document.write("<br />removed is: " + removed);

removed = arr.splice(3, 1);
document.write("<br />After adding 1: " + arr );
document.write("<br />removed is: " + removed);

</script>
</body>
</html>

This will produce following result:

After adding 1: orange,mango,water,banana,sugar,tea
removed is: 
After adding 1: orange,mango,water,sugar,tea
removed is: banana 

To understand it in better way you can Try it yourself.


previous next

Copyright © tutorialspoint.com