Back in 2012 at dotJS – which I attended – Brian Leroux gave his talk WTFJS. Somehow the video of that talk got recommended on YouTube to me. It was fun watching this again 🙂
Gotta love coercion 😍
Also: Wat?
A rather geeky/technical weblog, est. 2001, by Bramus
Back in 2012 at dotJS – which I attended – Brian Leroux gave his talk WTFJS. Somehow the video of that talk got recommended on YouTube to me. It was fun watching this again 🙂
Gotta love coercion 😍
Also: Wat?
0.1 + 0.2
results in 0.30000000000000004
)A few fun little Math-related wtfjs examples. A classic one is adding an empty Array and an empty Object:
[] + {} // '[object Object]'
{} + [] // 0
If you’re looking for a hint: Coercion.
(a ==1 && a== 2 && a==3)
ever evaluate to true
?Is it ever possible that
(a ==1 && a== 2 && a==3)
could evaluate totrue
, in JavaScript?This is interview question asked by a major tech company. My answer was that it’s impossible. They said nothing is impossible. It happened 2 weeks back, but I’m still trying to find the answer. I know we never write such code in our day to day job, but I’m curious.
Betteridge’s Law of Headlines demands the answer to this question to be no, but given the fact that it’s question about JavaScript the answer – of course – is yes.
Here’s a hint: note the ==
that’s used, allowing for coercion to happen 😉
StackOverflow: Can (a ==1 && a== 2 && a==3)
ever evaluate to true? →
Talk by Claudia Hernández, on a few of Javascript’s oddities and (un)expected behaviors:
There’s a presentation embedded in this post. View it on Speaker Deck.
For a language originally created in 10 days it surely has a lot of quirks and perks many JS developers are unaware of. Sometimes, it might even seem like we fell down the rabbit hole only to find that
NaN
is actually aNumber
,undefined
can be defined,+!![]
equals1
,Array.sort()
may not work as you suspected and so much other nonsense that can trip any JS developer’s mind.
Related: Equality in JavaScript, wtfjs, and – save best for last – wat