We're live-coding on Twitch! Join us!

Basics

This method takes out the first element from an array and returns it. The shift method mutates the array during this process, reducing its length.

[1, 2, 3, 4, 5].shift()
// 1

This method when called:

  • Removes the element at the beginning of the array.
  • Mutates the parent array reducing the length.
  • Returns the removed element.

Syntax

array.shift()

No Parameters

Returns a string

The method will return the element removed from the array. If the array is empty, it returns undefined.

const names = ['Johnny', 'Pete', 'Sammy']
console.log(names.shift());
// output: 'Johnny'

Common Usage and Snippets

Removing the index element from an array.

Like this article? Follow @codebeast on Twitter