Are you familiar with bit shifts? When you shift a 32-bit integer by one, for example, one bit will get shifted “off” the integer. In other words, the integer will have lost that bit.
Shift on an array similarly shifts an element off the array. Unshift puts an element back on the array.
Maybe more graphically: I you shift books on a shelf, the outermost book may fall off the shelf. To put it back on, you first need to “unshift” the books.
How I actually remember it is from Perl, where shift is commonly used to process the next argument from the argument array. And you rarely unshift.
Unshift, like push, adds an element to the array.
“Unshift”, like “push”, contains the letter “u”.
Certainly you will remember the difference now, after writing this.
I remember the same way I remember anything else — by memorizing.
In British English “shift” means “move,” so shifting something off an array means moving it (into a variable). Un- as a prefix indicates negation or reversing in English. So you just need to remember what shift does, and unshift does the opposite, as indicated by the word.
> Word "shift" is shorter than the word "unshift". Therefore shift() makes the array shorter.
> Word "unshift" is longer than the word "shift". Therefore unshift() makes the array longer.
This is quite useful as I too couldn't remember which is which. Thanks for sharing!
Unshift is a weird word for the weird method of the four. You’ve got a list. To use it as a stack you push and pop. To use it as a queue you push and shift. Unshift is the weird one.
Mix html with bit twiddle logic!
html < is open block, html > is close block.
< and << are logical shift left for array of bits.
> and >> are logical shift right for array of bits.
Looking at only 'logical value of 1'. Have to open/shift left before unopen/shift right to make thing correct. aka init variable to 0; shift left/push gives 1; shift right/removes aka pops the 1.
html < pushes/shifts all the commands onto stack.
html > pops all the commands todo off of the stack until reach < aka end of stack marker.
The easiest way is learning what shift does in Bash, like another comment said, but if you're not going to do that:
- C also has functions called getc() and ungetc(), and they're analogous.
- "Doing" and "undoing" are actually the same in some sense: doing causes information loss, undoing causes information gain.
as a 6y js/ts dev i never really bothered, i just end up coming back to mdn docs whenever im about to use those.
especially the internals of array sort method.
I have this problem with splice and slice too
bash is how i remember. shift consumes an argument starting at $1.
this is how it works in all languages
same concept as << (bit wise left shift)
Learn shell programming (https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.ht...) or MS-DOS (https://learn.microsoft.com/en-us/windows-server/administrat...), and it won’t be a problem remembering what shift does.
Neither of those has unshift, but you can infer its meaning from knowing about shift.
I also think push and pop aren’t good names. append, respectively removelast are way better.