JavaScript Glossary: Array .shift() Method

Written by Scotch.io - - Aggregated on Friday February 8, 2019

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

https://scotch.io/embed/gist/736d1e62476a0aed92ebe6b1b1b770dc

This method when called:

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.

https://scotch.io/embed/gist/cb772b7b64a09dd288fec7bf15846082


« Tips for Using Laravel’s Scheduler - Laravel News

Scotch.io - JavaScript Glossary: Array .reverse() … »