Home » JavaScript Array splice() Method

JavaScript Array splice() Method

by Online Tutorials Library

JavaScript Array splice() method

The JavaScript array splice() method is used to add/remove the elements to/from the existing array. It returns the removed elements from an array. The splice() method also modifies the original array.

Syntax

The splice() method is represented by the following syntax:

Parameter

start – It represents the index from where the method start to extract the elements.

delete – It is optional. It represents the number of elements to be removed.

element1,element2,…,elementn – It is optional. It represent the elements to be inserted.

Return

A new array containing the removed elements.

JavaScript Array splice() method example

Here, we will understand splice() method through various examples.

Example 1

Let’s see an example to add an element to the existing array without removing other elements.

Test it Now

Output:

Monday,Tuesday,Wednesday,Thursday,Friday  

Example 2

Let’s see an example to add an element to the existing array while removing other elements.

Test it Now

Output:

Updated array: Monday,Tuesday,Wednesday,Thursday,Friday  Removed element: Saturday,Sunday  

Example 3

Let’s see an example to add two elements to the existing array while removing one element.

Test it Now

Output:

Updated array: Monday,Tuesday,Wednesday,Thursday,Friday  Removed element: Sunday  

Example 4

Let’s see an example to remove the elements from the existing array.

Test it Now

Output:

Updated array: Monday,Tuesday  Removed element: Saturday,Sunday,Thursday,Friday  

Next TopicJavaScript Array

You may also like