DEV Community

Programming language quirks

Undo is to remove or reverse actions right!!!!

So why is unshift used to add to an array in JS? šŸ˜…

Continue the thread by commenting with yours in the comments section.
Keep it rolling

Top comments (6)

Collapse
 
morolawanf profile image
Morola

Before, in PHP, comparing "password" == 0 used to return true.
Entire login systems got walked right through.

Collapse
 
wrencalloway profile image
Wren Calloway

unshift actually makes sense once you know it's the mirror of shift, which is borrowed straight from Perl (and Unix shell before it). shift pulls the first argument off $@, so unshift puts one back on the front. The "un" isn't undo — it's the reverse operation on the same end of the list. JS just inherited the naming wholesale.

The real quirk is that push/pop and shift/unshift return different things: push gives you the new length, pop gives you the removed element. So you get bugs where someone writes const x = arr.push(y) expecting y back and gets a number instead.

Collapse
 
chocoscoding profile image
Chocoscoding - Oyeti Timileyin

wowwwww, I didn't see it like that.
Yeah, JS can be crazy šŸ« šŸ˜…šŸ˜…

Collapse
 
timileyin_wandf_84e4e43eb profile image
Timileyin WandF

in SQL:

NULL = NULL evaluates to NULL (not TRUE). Equality itself becomes uncertain.
🄲

Collapse
 
chocoscoding profile image
Chocoscoding - Oyeti Timileyin

Lol. 🫠

DYK that [] == ![] is true in Javascript?

Collapse
 
sodalin23 profile image
Sodalin • Edited

This reminded me that every language has its own personality. Some of these quirks seem odd at first, but they're often the things you remember the most when switching between languages. Nice post!