How to use the shift() method In Typescript

The shift() method is used to remove the item from the start of an array and it returns the removed item as result. It requires no parameters.

Syntax:

const remItem = array.shift();

Example: The below example will explain the use of the shift() method to remove element from TypeScript array.

Javascript
const testingArr: (number | string)[] =
    ["JavaScript", 1, "w3wiki", 2, "TypeScript"];

console.log(`Initial Array: ${testingArr}`);
// Using the shift() method
const remItem = testingArr.shift();
console.log(`Removed Item: ${remItem}`)
console.log(`Array After removing item: ${testingArr}`);

Output:

Initial Array: JavaScript, 1, w3wiki, 2, TypeScript
Removed Item: JavaScript
Array After removing item: 1, w3wiki, 2, TypeScript

How do I Remove an Array Item in TypeScript?

In this article, we will learn about the different ways of removing an item from an array in TypeScript. In TypeScript, an array can be defined using union typing if it contains items of different types.

We can use the following methods to remove items from a TypeScript array:

Table of Content

  • Using the splice() method
  • Using the shift() method
  • Using the pop() method
  • Using filter() method
  • Using the delete operator
  • Using the slice() method

Similar Reads

Using the splice() method

The splice method can be used to delete multiple elements by specifying the start index from where the deletion of the element will start and the number of elements to be deleted as parameters to it. This method will return the removed elements....

Using the shift() method

The shift() method is used to remove the item from the start of an array and it returns the removed item as result. It requires no parameters....

Using the pop() method

The pop() method will delete the last element of the array without passing any parameter. It returns the removed element as a result....

Using filter() method

The filter() method filter the array based on the condition of the passed callback function. It returns a new array with new length and new elements....

Using the delete operator

The delete operator can also be used to delete the array items in TypeScript....

Using the slice() method

The slice() method can be utilized to remove an item from an array by creating a new array without the item to be removed. This method is useful when you want to keep the original array intact and obtain a modified array without modifying the original one....