JavaScript Tutorials

JavaScript Array splice()
Syntax & Examples

Array.splice() method

The splice() method of the Array class in JavaScript adds and/or removes elements from an array.


Syntax of Array.splice()

There are 5 variations for the syntax of Array.splice() method. They are:

1.
splice(start)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array.

This method removes all elements from the start index to the end of the array.

Returns value of type Array.

2.
splice(start, deleteCount)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array.
deleteCountoptionalThe number of elements to remove from the start index.

This method removes the specified number of elements from the start index.

Returns value of type Array.

3.
splice(start, deleteCount, item1)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array.
deleteCountoptionalThe number of elements to remove from the start index.
item1optionalThe element to add to the array, beginning at the start index.

This method removes the specified number of elements from the start index and adds the specified item.

Returns value of type Array.

4.
splice(start, deleteCount, item1, item2)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array.
deleteCountoptionalThe number of elements to remove from the start index.
item1optionalThe first element to add to the array, beginning at the start index.
item2optionalThe second element to add to the array, beginning at the start index.

This method removes the specified number of elements from the start index and adds the specified items.

Returns value of type Array.

5.
splice(start, deleteCount, item1, item2, /* …, */ itemN)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array.
deleteCountoptionalThe number of elements to remove from the start index.
item1optionalThe first element to add to the array, beginning at the start index.
item2optionalThe second element to add to the array, beginning at the start index.
itemNoptionalAdditional elements to add to the array, beginning at the start index.

This method removes the specified number of elements from the start index and adds the specified items.

Returns value of type Array.



✐ Examples

1 Using splice() method to remove elements from an array

In JavaScript, we can use the splice() method to remove elements from an array.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. We use the splice() method on arr with the start index 1 and deleteCount 2 to remove two elements starting from index 1.
  3. The result is stored in the variable removedElements.
  4. We log removedElements and the updated array to the console using console.log() method.

JavaScript Program

const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2);
console.log(removedElements);  // Output: [2, 3]
console.log(arr);              // Output: [1, 4, 5]

Output

[2, 3]
[1, 4, 5]

2 Using splice() method to add elements to an array

We can use the splice() method to add elements to an array.

For example,

  1. We define an array variable arr with elements ['a', 'b', 'e'].
  2. We use the splice() method on arr with the start index 2, deleteCount 0, and items 'c' and 'd' to add the elements 'c' and 'd' at index 2.
  3. The result is stored in the variable removedElements.
  4. We log removedElements and the updated array to the console using console.log() method.

JavaScript Program

const arr = ['a', 'b', 'e'];
const removedElements = arr.splice(2, 0, 'c', 'd');
console.log(removedElements);  // Output: []
console.log(arr);              // Output: ['a', 'b', 'c', 'd', 'e']

Output

[]
['a', 'b', 'c', 'd', 'e']

3 Using splice() method to replace elements in an array

We can use the splice() method to replace elements in an array.

For example,

  1. We define an array variable arr with elements [1, 2, 3, 4, 5].
  2. We use the splice() method on arr with the start index 1, deleteCount 2, and items 'a' and 'b' to replace two elements starting from index 1 with 'a' and 'b'.
  3. The result is stored in the variable removedElements.
  4. We log removedElements and the updated array to the console using console.log() method.

JavaScript Program

const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2, 'a', 'b');
console.log(removedElements);  // Output: [2, 3]
console.log(arr);              // Output: [1, 'a', 'b', 4, 5]

Output

[2, 3]
[1, 'a', 'b', 4, 5]

Summary

In this JavaScript tutorial, we learned about splice() method of Array: the syntax and few working examples with output and detailed explanation for each example.