Videos
EDIT: This is just venting, not confusion on how the word is technically used by JS :D
OK, this is why I keep starting and stopping learning JS- reserved words with extremely unrelated English meanings. "Unshift" means a previous shift occurred. That's the only meaning for it in English. But JS decided to use it as a prepend array method. Argh!!
That is all...
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
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.
Javascript has a number of methods related to arrays which allow programmers to perform various array operations. There are four methods which are particularly used for adding and removing elements to and from an array. They are: push(), pop(), shift() and unshift(). For an experienced as well as new programmers, its likely to sometimes get confused how each of them work and which one to use in which situation. Thus, in this article, we have tried to simplify the concept with pictures and examples. Let's start exploring them one by one. Then we will compare their similarities and differences. Please look at the pictures too for better understanding.
https://foxbits.dev/article/four-common-javascript-array-methods-push-pop-shift-and-unshift/15
Shouldn't unshift() remove, and shift() add?
My train of thought is that "I will shift something into the beggining" and "unshift it from the beggining."
This makes no sense.
Yikes.