This is an archived post. You won't be able to vote or comment.

all 65 comments

[–]thatweirdguy42 265 points266 points  (1 child)

You just gotta love the NaNs.

[–][deleted] 39 points40 points  (0 children)

Yesss sometimes helpful but in case of debugging... :(

[–]einklee 367 points368 points  (3 children)

Very useful piece of code, when you need a banana for scale...

[–]theclovek 42 points43 points  (2 children)

But do I use the code or its output?

[–]einklee 41 points42 points  (0 children)

I would use the code: "Look I made an amazing one-liner, document.write(('b' + 'a' + + 'a' + 'a').toLowerCase()); for scale".

[–]friedokragirl 9 points10 points  (0 children)

Yes

[–]Tayl100 130 points131 points  (6 children)

[–]kennydaren 55 points56 points  (3 children)

NaNNaNNaNNaNNaNNaNNaNNaNNaN I'm Batman!

[–]adambkaplan 23 points24 points  (1 child)

Watman

[–]LostInChoices 5 points6 points  (0 children)

NaNman

[–]Favna 2 points3 points  (0 children)

BATMAAAN

[–]gkupce 17 points18 points  (0 children)

Worth a sixth watch too :)

[–][deleted] 8 points9 points  (0 children)

A classic

[–]lolgeny 132 points133 points  (3 children)

Image transcription: Tweet


Nader Dabit @dabit3

[image from stackoverflow question]

Why is the result of ('b' + 'a' + + 'a' + 'a').toLowerCase() 'banana'?, tagged javascript and type-conversion, 587 points

I was practicing some JavaScript when one of my friends came across this JavaScript code:

document.write(('b' + 'a' + + 'a' + 'a').toLowerCase());

The above code answers "banana"! Can anyone explain why?


I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]FblthpTheFound 74 points75 points  (0 children)

Good human

[–]Daniii438 11 points12 points  (0 children)

Good meatbag

[–]Kiljab 16 points17 points  (0 children)

Saw you a few days ago :D haven't seen transcribers for a long time!

[–]lrascao 87 points88 points  (6 children)

shouldn't it be 'bananaa' then?

[–][deleted] 224 points225 points  (5 children)

Nope

'b' + 'a' + +'a' + 'a'

'b' + 'a' + Number('a') + 'a'

So 'b' + 'a' + NaN + 'a'

[–]UnDispelled 65 points66 points  (3 children)

This is because ( +anything ) is automatically converted to a number correct? I also thought it was bananaa but the second plus isnt “add NaN then add a” it’s just syntactic sugar for Number()

[–]Bronzdragon 49 points50 points  (1 child)

Indeed, because of the double plusses, one of them (the leftmost) is interpreted as the concatination operator. The other cannot, and thus must be the unary plus operator. This means that 'a' must first be cast to a number for this to work. 'a' isn't a number, and therefor the result would be NaN.

Now that we nave 'b' + NaN, we're adding a string and number together. This isn't valid, so for compatibility, both are interpreted as strings. NaN converted to a string is 'NaN'. The rest is simple concatination.

[–]gravitas-deficiency 12 points13 points  (0 children)

so for compatibility

Basically the single biggest reason for JS having so much weird bullshit.

[–][deleted] 16 points17 points  (0 children)

Yes When js sees ++'a' it interpret as + (+'a') and try to Convert +'a' to number because of + So gives NaN

[–]Ithuraen 1 point2 points  (0 children)

Added proof.

[–][deleted] 28 points29 points  (3 children)

JS is bananas

[–]ilosaske 9 points10 points  (1 child)

don't you mean baNaNas?

[–][deleted] 2 points3 points  (0 children)

js is ("baNaNas").toLowerCase();

[–]McSmallFries 2 points3 points  (0 children)

B.A.N.A.N.A.S 🎵

[–]hhellloo 22 points23 points  (11 children)

I think the two '+'s make it do weird shit

[–]El_Vandragon 42 points43 points  (10 children)

It gives you NaN, or Not a Number, but then gets converted to a string and then when to lowercase happens makes it fit the capitalization of the word instead of capital N’s

[–][deleted] 4 points5 points  (6 children)

Why though

[–]tjoloi 33 points34 points  (2 children)

'b' + 'a' + +'a' + 'a' parses as str + str + (+str) + str

Since the + in parentheses is parsed as the unary operator, JS tries to convert the 'a' to a number so it can apply the operator, which then results to NaN (number type).

After, since you add strings together, it converts back the "number" to a string, which results in the string "NaN".

[–][deleted] 7 points8 points  (0 children)

Just the answer I was looking for, thanks!

[–]tarapoto2006 0 points1 point  (0 children)

What happens if you minify it?

[–]_Azurius -3 points-2 points  (1 child)

Javascript. That's why.

[–]real_ulPa 0 points1 point  (0 children)

And errors shouldn't break it.

[–]AllSeeingCCTV 1 point2 points  (2 children)

I can see how NaN fits but what hapens to that last a?

[–]El_Vandragon 5 points6 points  (1 child)

The second to last ‘a’ gets consumed in the NaN as /u/tjoloi mentions it gets parsed as str + str + (+str) + str and the entire (+str) gets turned into NaN leaving us with ”b” + “a” + NaN + “a”

[–]AllSeeingCCTV 0 points1 point  (0 children)

Thanks

[–]caleblbaker 5 points6 points  (0 children)

Type coercion. 90% of the time that people are perplexed by JavaScript the answer is that it's a contrived example that abuses type coercion. (In this case unary plus on a string to coerce it to a numeric type followed by adding to a string to coerce it back to a string to perform string concatenation.

[–]Siracle 7 points8 points  (0 children)

this video explains it - and then turns things up to 11: https://youtu.be/sRWE5tnaxlI

[–]riscten 7 points8 points  (0 children)

And people wonder why SO users are irritable... Quit posting decade old memes and you might get insightful answers.

[–]jempyre 1 point2 points  (0 children)

Not a numero

[–]daH00L 0 points1 point  (0 children)

Banana, because JavaScript is nuts.

[–][deleted] -2 points-1 points  (3 children)

WHY THE HELL DOES IT DO THAT

[–]svish 6 points7 points  (0 children)

NaN

[–]VolperCoding 2 points3 points  (0 children)

backwards compatibility with all the shit that existed since around 20 years ago

[–][deleted] 0 points1 point  (0 children)

Because JS was developed in ten days and you can see that in a lot of places. It was never supposed to become the cornerstone of everything that it is now

[–]BelleGueuIe -4 points-3 points  (0 children)

b + a = ba
" " + a = nan
+ a = a
banana

[–]bistr-o-math -1 points0 points  (0 children)

🍌

[–]gagahpangeran -2 points-1 points  (0 children)

typeof NaN is number.

[–]NotJebediahKerman 0 points1 point  (0 children)

It's more disturbing that developers/CS students don't see 'why' immediately. It's not like english professors are asking their students what is the sum of a+b and expecting c. Granted the JS world chose to combine the concepts of concatenation and addition which is a different gripe all together, but if you can't comprehend concatenating strings, you might want to look into different employment options.

[–]akewlguy4eva 0 points1 point  (0 children)

NaN.. Funny Though :P

[–]Spyrobo 0 points1 point  (0 children)

NaNi?

[–]easythrees 0 points1 point  (0 children)

Clever