JavaScript Array splice() Method

The JavaScript Array.splice() method is used to add, remove, or replace elements in an array by modifying the original array. It changes the content of an array by removing existing elements and/or adding new elements in their place.

For example, array.splice(start, deleteCount, item1, item2, ...) is useful when you need to modify an array by removing or inserting elements.

Syntax

The syntax for the Array.splice() method is:

plaintext
array.splice(start, deleteCount, item1, item2, ...)

Parameters

The Array.splice() method accepts three parameters:

  • start (Required): The index at which to start changing the array. If negative, it starts from the end of the array.
  • deleteCount (Optional): The number of elements to remove from the array, starting from start. If 0, no elements are removed.
  • item1, item2, … (Optional): The elements to add to the array, starting from the start index. If no elements are specified, splice() only removes elements.

Return Value

The method returns an array containing the deleted elements. If no elements are removed, it returns an empty array.

The original array is modified in place.

Examples of JavaScript Array.splice() Method

Example 1: Removing Elements from an Array

You can use the splice() method to remove elements from an array.

javascript
const fruits = ['apple', 'banana', 'mango', 'orange'];
const removedFruits = fruits.splice(1, 2);
console.log(fruits);  // Output: ['apple', 'orange']
console.log(removedFruits);  // Output: ['banana', 'mango']

Explanation: The splice(1, 2) method removes two elements starting from index 1, which are 'banana' and 'mango'. The remaining elements in the fruits array are ['apple', 'orange'].

Example 2: Adding Elements to an Array

You can use splice() to insert new elements into an array at a specific index.

javascript
const numbers = [10, 20, 30];
numbers.splice(1, 0, 15, 25);
console.log(numbers);  // Output: [10, 15, 25, 20, 30]

Explanation: The splice(1, 0, 15, 25) method inserts 15 and 25 starting at index 1 without removing any elements. The deleteCount is 0, so no elements are deleted.

Example 3: Replacing Elements in an Array

You can replace existing elements in an array using splice() by specifying the number of elements to delete and adding new elements.

javascript
const fruits = ['apple', 'banana', 'mango'];
fruits.splice(1, 1, 'grape', 'kiwi');
console.log(fruits);  // Output: ['apple', 'grape', 'kiwi', 'mango']

Explanation: The splice(1, 1, 'grape', 'kiwi') method removes 'banana' (at index 1) and inserts 'grape' and 'kiwi' in its place, resulting in ['apple', 'grape', 'kiwi', 'mango'].

Example 4: Removing Elements Without Adding New Ones

You can remove elements from an array without adding any new elements.

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

Explanation: The splice(2, 2) method removes two elements starting at index 2 (3 and 4), leaving the array as ['1, 2, 5'].

Example 5: Using Negative Indices with splice()

You can use negative indices to start splicing from the end of the array.

javascript
const numbers = [10, 20, 30, 40, 50];
numbers.splice(-2, 1);
console.log(numbers);  // Output: [10, 20, 30, 50]

Explanation: The splice(-2, 1) method removes one element starting from the second-to-last element (40), resulting in ['10, 20, 30, 50'].

Example 6: Removing All Elements from an Index

You can remove all elements starting from a specific index by omitting the deleteCount.

javascript
const fruits = ['apple', 'banana', 'mango', 'grape'];
fruits.splice(2);
console.log(fruits);  // Output: ['apple', 'banana']

Explanation: The splice(2) method removes all elements from index 2 onwards ('mango' and 'grape'), leaving only ['apple', 'banana'].

JavaScript

8469

737

Related Articles