Array in JavaScript – splice() Method

Array in JavaScript has splice() method that changes the elements of an array, adding new elements as well as removing old.

Syntax is as below:

index: index at which to start changing the array. Starting at 0.
howManyElements: An integer indicating the number of old array elements to remove. Set howManyElements to 0 to not remove any element.
item1, item2, itemN: The elements to add to the array. Not specifying any elements will let splice simply remove elements from the array.

Return Value:

splice() method returns the extracted array based on the passed parameters.

Example:

Output: This will give the following output:

To understand the splice() method in better way Try yourself.

So splice() method for Array in JavaScript is powerful method that let you modify an existing array. You can add, remove elements or replace existing elements.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.