JavaScript: array.shift()
vs array.unshift()
shift()
removes the first array item,
and unshift()
prepends items to the array.
But how to remember which is which?
Here's a mnemonic:
The methods with shorter names remove items.
So, pop()
and push()
:
pop()
has a shorter name thanpush()
pop()
removes the last array itempush()
appends items to the array.
Similarly, shift()
vs unshift()
:
shift()
has a shorter name thanunshift()
shift()
removes the first array itemunshift()
prepends items to the array.