JavaScript Array shift() Tutorial

In this section, we will learn how to remove an element from the start position of an array.

JavaScript Arrays shift() Method

In JavaScript, there are multiple ways of removing elements from an array. One of them is calling the pop() method. This method basically removes the last element of an array (which is on the right side of that array) and returns that as a result.

But what if we want to remove elements from the start position of an array then? (The first element on the left side)

This is where we can use the `shift()` method.

The `shift()` method removes the first element on the left side of an array and returns that element as a result.

Array shift() Syntax:

array.shift();

Array shift() Method Parameters:

The method does not take an argument.

Array shift() Method Return Value:

The return value of the shift() method is the first element of an array (the first element on the left side) that was removed as a result of calling this method.

Example: using Array shift() method in JavaScript

const arr = ["Tesla","Google","Microsoft","Amazon","Facebook","Twitter"];
const size = arr.length;
for(let i = 0 ; i<size; i++){
  console.log(`The length of the array is: ${arr.length}`);
  console.log(arr.shift());
}

Output:

The length of the array is: 6

Tesla

The length of the array is: 5

Google

The length of the array is: 4

Microsoft

The length of the array is: 3

Amazon

The length of the array is: 2

Facebook

The length of the array is: 1

Twitter

Leave a Reply