32

I am having hard time remembering what Array.shift() and Array.unshift() do.

After few years, I still have too check out reference from time to time when I need to use one of them. Can anyone explain why those names are choosen and how to memorize which one does what?

I have no such problem with Array.push() and Array.pop()

7
  • 42
    Easy to remember if you mentally drop the "f" in shift / unshift: shift removes elements and unshift adds them :) Commented Mar 28, 2016 at 8:22
  • 2
    @VickyChijwani I would flag your comment as hilarious :D Commented Oct 18, 2017 at 16:18
  • 1
    Without a doubt @VickyChijwani comment is the best answer to the question of how to remember what they do Commented Jul 21, 2018 at 0:15
  • 2
    Just remember the "Shift" is shorter than "Unshift", "Shift" is also going to make the Array shorter as well. I think the problem with remembering is not whether it adds from front or back, its which adds/removes from the front
    – Alex L
    Commented Dec 5, 2020 at 17:00
  • 1
    The easiest way to remember the difference: The longer words (push, unshift) make the array longer, the shorter words (pop, shift) make it shorter.
    – Giregar
    Commented Jan 2, 2022 at 19:33

5 Answers 5

22

As I known.

The shift command is come from the binary bit shift [1]. for Example.

    001100
0 < 011000 // when you shift left
|
Yay!

I think it is quite simple it is just like you push it from behind. So this makes sense for me.

The unshift is the opposite way of shift.

    001100
1 > 001100 // they called it unshift
    1001100
    |
    Yay!

So that's it, Hope this helps!

[1] http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts

5
  • So that is the problem! Because I can think as shift: 001100 < 1 = 0011001, and unshift: 001100 -> = 00110 + 0. So the question is how to memorize the direction? Push/pop are easy, push goes in, pop goes out.
    – exebook
    Commented Oct 1, 2013 at 4:21
  • @Chokchai—Not a good analogy. Shift removes the first member of the array and returns it, unshift adds the supplied arguments to the start of an array in the order they are presented and returns the new length of the array.
    – RobG
    Commented Oct 1, 2013 at 4:24
  • 3
    @exebook—there is no simple way to remember it, just remember where to find it in the spec and look it up if in doubt.
    – RobG
    Commented Oct 1, 2013 at 4:25
  • This is a great explanation. Personally I prefer enqueue and dequeue. Commented Dec 15, 2015 at 3:35
  • @RobG It's not an analogy, it's where the name actually comes from (OP asked Can anyone explain why those names are choosen)
    – s-ol
    Commented Sep 29, 2016 at 11:59
9

a.push(e) pushes e onto the end of a.

e = a.pop() pops the last element from a, to e.

a.unshift(e) enqueues e to the start of a.

e = a.shift() gets the first element from a to e.

Use push and pop for stacks.

Use unshift and pop for queues. (Or push and shift)

I remember the difference between shift (destructive) and unshift (constructive) simply by remembering that I use un-shift for en-queueing, and shift is the opposite to unshift.

2
  • 2
    Yes, I know what they do... But tomorrow I will forget which one does what. I tried many times!
    – exebook
    Commented Oct 1, 2013 at 4:29
  • @exebook Apologies, see edit.
    – azz
    Commented Oct 1, 2013 at 4:30
9

Just think of your keyboard:

Shift gets a capital version of the first key you press.

.shift() gets the first item off the array.

1
  • This is simple enough for me.
    – Sethen
    Commented Aug 6, 2015 at 20:34
5

How about:

SHIFTer makes a drifter

It returns the first entry to the variable.

and -

UNSHIFTer is a weenier that sneaks in line

Inserts argument as first entry in array

Oh, there are deep psychological techniques at work here!! :-o But seriously, you will remember it for its peculiarity :-)

1
  • 1
    Of all of the answers I actually like this best. Push/Pop are easy, and I can just remember "shifter makes a drifter" since it leaves you with something drifting out of the array.
    – Owen Allen
    Commented Jan 29, 2014 at 18:05
4

This is certainly the most confusing pair of function names. The only salvation I can offer is to remember one of the following two things:

  • Shift can be thought of as "moving something around," and perhaps you can picture that if you "shift" an array around a bunch, something is liable to fall off the end (or in this case, the beginning). Unshift puts things back the way they were.
  • It's the opposite of what it sounds like it should be. unshift sounds like undoing something, but in fact, it's putting something onto the array.

Good luck!

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.