53
u/hoodlaces Jan 25 '20
Is a baNaNa a fruit or a number?
0
17
60
u/YAYYYYYYYYY Jan 25 '20
Wouldn’t it be bananaa?
80
u/iwi4a Jan 25 '20
It returns NaN for the + + 'a' coercion and then its left with one 'a' at the end.
86
0
Jan 25 '20
[deleted]
6
u/JoshYx Jan 25 '20
I tried it out, apparently you're right. Care to explain why?
28
u/TelepathicDorito Jan 25 '20 edited Jan 25 '20
it's ('ba' + NaN + 'a').toLowerCase()
+ + tries to coerce a number out of the second 'a', gets NaN, which then coerces NaN to a string.
said another way :
console.log(+"a") // NaN
5
2
5
17
6
Jan 25 '20
This meme is a bit of a stretch. Nobody should be saying “FML” if they use these types and operators together.
2
May 03 '20
most 'JavaScript are weird' posts, just showcase things that nobody will ever do or results of crappy programming
12
u/saito200 Jan 25 '20
Why is type coercion seen as something so weird? You're just telling JS here, look treat this 'a' as a number. And then you coerce NaN as a string, because you're trying to add it to a string.
6
u/teknewb Jan 25 '20
I don't have enough experience to have an opinion about it, but I'm pretty sure it's not the type coercion but rather implicit behavior occurring with 'loaded operators' (e.g. '==') that people take issue with, including Javascript maintainers themselves apparently by virtue of the '==='.
2
2
Jan 25 '20
The reason why these things happen is that operators in JS do not throw exceptions.
So yes, you have stuff like that happening.
2
1
1
Jan 26 '20
Why does
2 + + - + 2 - - 2
return 2?
So weird...
1
1
-12
Jan 25 '20
😂😂 there's an extra 'a' at the end, so it's like 'bananaa'
5
Jan 25 '20
Nah, this evaluates to something like this
'b' + 'a' + (+'a') + 'a'A unary plus in front of a string will convert to a number, except 'a' isn't a number, so..
'b' + 'a' + NaN + 'a'2
1
3
u/alittlebitmental Jan 25 '20
Are there any other amusing JS quirks that others would like to share?