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:
- 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.
https://scotch.io/embed/gist/cb772b7b64a09dd288fec7bf15846082