JavaScript Arrays: Shift
Welcome to the Shift lesson!
JavaScript arrays' "shift" and "unshift" methods are similar to "push" and "pop", but they operate on the first element.
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
We've seen
.pushand.pop. They add and remove elements on the end of the array..shiftand.unshiftdo the same, but for the beginning of the array..shiftremoves the first element of the array and returns it.>
const a = ['a', 'b'];a.shift();a;Result:
['b']
>
const a = ['a', 'b'];a.shift();Result:
'a'
.unshiftadds an element to the beginning of an array.>
const a = ['a', 'b'];a.unshift('z');a;Result:
['z', 'a', 'b']
.unshiftreturns the array's length, including the newly-added element. This matches.push, which also returns the array's length.>
const a = ['a', 'b'];a.unshift('z');Result:
3
.shiftand.unshifthave somewhat confusing names. Here's one way to remember them..shift"shifts" all of the elements down by one. Index 2 becomes index 1; index 1 becomes index 0; etc..unshiftdoes the opposite.Here's a demonstration.
>
const a = ['a', 'b', 'c'];const origIndex = a.indexOf('c');a.shift();const newIndex = a.indexOf('c');origIndex + ' becomes ' + newIndex;Result:
'2 becomes 1'
Calling
.unshiftputs the new element in index 0. This means that the existing elements all move to the right by one. Their indexes all increase.>
const a = ['a', 'b'];const origIndex = a.indexOf('b');a.unshift('z');const newIndex = a.indexOf('b');origIndex + ' becomes ' + newIndex;Result:
'1 becomes 2'
Like
.popand.push,.shiftand.unshiftchange the original array.>
const a = [1, 2, 3];a.shift();a.shift();a;Result:
[3]
>
const a = [1, 2, 3];a.shift();a.unshift('a');a;Result:
['a', 2, 3]