Javascript shift() unshift() mnemonics? [closed]
As I known.
The shift command is come from the binary bit shift [1]. for Example.
0011000 < 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.
0011001 > 001100 // they called it unshift 1001100 | Yay!
So that's it, Hope this helps!
[1] http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
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
.
Just think of your keyboard:
Shift gets a capital version of the first key you press.
.shift()
gets the first item off the array.