Open In App

JavaScript Array shift() Method

Last Updated : 18 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The shift() method in JavaScript is used to remove the first element of an array, reducing the array’s length by one. This method is particularly useful for scenarios where elements need to be processed in the order they were added, such as in queue-like structures.

Syntax:

arr.shift();

Parameters:

  • This method does not accept any parameter.

Return Value:

  • This function returns the removed first element of the array. If the array is empty then this function returns undefined.

Note: This function can also be used with other javascript objects that behave like the array.

Key Points

  • The shift() method modifies the original array.
  • It can be used with other JavaScript objects that behave like arrays (e.g., array-like objects).
  • Indices of the remaining elements are adjusted, decrementing by one to fill the gap left by the removed element.

Examples of Array shift() Method

Example 1: Removing the First Element from the Array

The function func() removes the first element from array using the shift() method. The removed element is stored in the variable, value, which is then logged to the console along with the modified array.

// Original array
let array = ["GFG", "Geeks", "for", "Geeks"];

// Checking for condition in array
    let value = array.shift();

console.log(value);
console.log(array);

Output
GFG
[ 'Geeks', 'for', 'Geeks' ]

Example 2: Removing First Element from Empty Array

The function func() attempts to remove the first element from an empty array array using the shift() method. Since the array is empty, shift() returns undefined, which is logged to the console along with the unchanged array.

// Original array
let array = [];

// Checking for condition in array
let value = array.shift();

console.log(value);
console.log(array);

Output
undefined
[]

Example 3: Removing the First Element from the Nested Array

The function func() removes the first element from the nested array using the shift() method. The removed element is stored in the variable value, which is then logged to the console along with the modified array.

// Original array
let array = [1,[2,3,4],5,6];

// shift method on nested array
let value = array[1].shift();

console.log(value);
console.log("Array after operation: "+ array);

Output
2
Array after operation: 1,3,4,5,6

Supported Browsers:

JavaScript Array shift() Method – FAQs

What does the Array.prototype.shift() method do in JavaScript?

The Array.prototype.shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

What happens to the indices of the remaining elements after using shift()?

The indices of the remaining elements are decremented by one to fill the gap left by the removed element.

Can shift() handle arrays of different data types?

Yes, shift() can handle arrays containing elements of any data type, including strings, numbers, objects, and other arrays.

What happens if you use shift() on an empty array?

If you use shift() on an empty array, it returns undefined and the array remains unchanged.

What are common use cases for the shift() method?

  • Removing Elements from the Beginning: Using shift() to remove the first element of an array.
  • Implementing Queue-like Structures: Managing data structures like queues where elements are processed in the order they were added (FIFO – First In, First Out).
  • Real-time Data Processing: Continuously removing and processing the first element of an array in applications like streaming data.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Next Article

Similar Reads

three90RightbarBannerImg