TypeScript Array splice()
Syntax & Examples

Array.splice() method

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. This method modifies the original array and returns the removed elements.


Syntax of Array.splice()

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

1.
splice(start: number, deleteCount?: number): T[]

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array. If negative, it will begin that many elements from the end of the array.
deleteCountoptionalThe number of elements to remove from the array. If omitted, all elements from start to the end of the array are removed.

This method removes elements from the array starting from the specified index, and returns an array of the removed elements. If deleteCount is not specified, all elements from start to the end of the array are removed.

Returns value of type Array.

2.
splice(start: number, deleteCount: number, ...items: T[]): T[]

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index at which to start changing the array. If negative, it will begin that many elements from the end of the array.
deleteCountrequiredThe number of elements to remove from the array. If omitted, all elements from start to the end of the array are removed.
itemsoptionalThe elements to add to the array, beginning at the start index.

This method removes elements from the array starting from the specified index, and inserts new elements in their place. Returns an array of the removed elements.

Returns value of type Array.



✐ Examples

1 Using splice() to remove elements from an array

In TypeScript, you can use the splice() method to remove elements from an array, starting at a specified index.

For example,

  1. Define an array arr with some initial values.
  2. Use the splice() method to remove 2 elements starting from index 1.
  3. Store the removed elements in the variable removedElements.
  4. Log arr and removedElements to the console using the console.log() method.

TypeScript Program

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

Output

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

2 Using splice() to replace elements in an array

In TypeScript, you can use the splice() method to replace elements in an array, starting at a specified index.

For example,

  1. Define an array arr with some initial values.
  2. Use the splice() method to remove 1 element starting from index 2, and insert 'a' and 'b' in its place.
  3. Store the removed element in the variable removedElement.
  4. Log arr and removedElement to the console using the console.log() method.

TypeScript Program

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

Output

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

3 Using splice() to add elements to an array without removing any

In TypeScript, you can use the splice() method to add new elements to an array, starting at a specified index, without removing any elements.

For example,

  1. Define an array arr with some initial values.
  2. Use the splice() method to insert 'a' and 'b' at index 3 without removing any elements.
  3. Log arr to the console using the console.log() method.

TypeScript Program

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

Output

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

Summary

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