I’ve always had trouble distinguishing between the
pop methods are just obvious to me.
I got inspired by a tutorial from the Execute Program. Here is their take on this:
unshifthave somewhat confusing names. Here is 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.
It is very clever to think about these methods in terms of the changes in existing elements’ indices. But it didn’t completely click with me. How can I remember that
shift decrements the indices and
unshift increments them?
For this, I chose to think of
unshift as upshift (a shift to a higher gear).
Now it becomes clear that
unshift (aka upshift) (adding elements to the beginning) will “shift” the existing array element’s indices up, but
shift (removing an element from the beginning) - down.
Update: It occurred to me that you can also think of the “shift” of array’s length instead of element indices. 👍