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.
Technology inspires me to learn, create, and improve every day. I enjoy transforming ideas into code, exploring new tools, and embracing every challenge as an opportunity to grow. I believe that conti
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!
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (6)
Before, in PHP, comparing "password" == 0 used to return true.
Entire login systems got walked right through.
unshiftactually makes sense once you know it's the mirror ofshift, which is borrowed straight from Perl (and Unix shell before it).shiftpulls the first argument off$@, sounshiftputs 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/popandshift/unshiftreturn different things:pushgives you the new length,popgives you the removed element. So you get bugs where someone writesconst x = arr.push(y)expectingyback and gets a number instead.wowwwww, I didn't see it like that.
Yeah, JS can be crazy š« š š
in SQL:
NULL=NULLevaluates toNULL(not TRUE). Equality itself becomes uncertain.š„²
Lol. š«
DYK that
[] == ![]istruein Javascript?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!