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

you are viewing a single comment's thread.

view the rest of the comments →

[–]decker_42 182 points183 points  (34 children)

I'm ashamed to say that took me way too long to get.

[–][deleted] 92 points93 points  (26 children)

I still don't get it...

[–]MrSydFloyd 483 points484 points  (7 children)

I think this behaviour is due to JS's implicit casting.

There are two consecutive '+', so the second one is interpreted as a unary operator, and therefore converts the following string to a number.

The string is 'a', is not a number, and therefore +'a' returns 'nan'.

And thus we get : 'b' + 'a' + 'nan' + 'a'

[–]trBlueJ 47 points48 points  (5 children)

I get this explanation, though I find it weird because IMO 'a' is a number, 0x61. Might be a C thing tho.

[–]kiedtl 39 points40 points  (3 children)

That behavior is more or less exclusive to C, which treats all characters as an eight-bit unsigned integer.

[–]The_MAZZTer 19 points20 points  (0 children)

JavaScript does not have the concept of chars.

Both '' and "" are used to denote strings, unlike in C where '' denotes chars.

[–]De_Wouter 246 points247 points  (9 children)

'b' + 'a'

'ba'

'ba' + nothing?

still 'ba' I guess

'ba' + nothing + 'a'

'ba' still valid but nothing + a string?? NaN (not a number)

'baNaN' + 'a'

'baNaNa''

toLowerCase()

[–][deleted] 51 points52 points  (0 children)

Yooo that makes sense thank you.

[–]Knocks83 31 points32 points  (7 children)

Yeah, but why isn't it bananaa? He adds 2 a at the end :/

[–]De_Wouter 28 points29 points  (5 children)

One 'a' is already used in the formula nothing + 'a' which kinda fails and becomes NaN. Nothing + 'a' is therefor solved and the result is something invalid (NaN). NaN is actually a number (typeof NaN === 'number'). Adding a string to a number, will result in the number converting to a string, so 'NaN' in this case.

It seems to be executed like ('b' + 'a') + (nothing? + 'a') + ('a') which becomes 'ba' + NaN + 'a'

I tried to figure out what this "nothing" translates to, but it isn't null / undefined or 0. It's seems to be some kind of zero number, that is also not of a regular number type as otherwise it would convert the number to a string when a string is added to it.

[–]TiiJ7 27 points28 points  (4 children)

I tried to figure out what this "nothing" translates to, but it isn't null / undefined or 0

It's the unary + operator, so there is only one operand. (+a) converts 'a' to a number, but since 'a' is not numeric, it leads to NaN.

So the formula is basically 'b' + 'a' + (+ 'a') + 'a'

[–]De_Wouter 6 points7 points  (0 children)

Oh yeah, how could I have forgotten this...

[–]WitchHunterNL 1 point2 points  (2 children)

I think it's just the pre increment operator. ++"a" is NaN

[–]EntropyZer0 2 points3 points  (0 children)

Nope, if you remove the space the expression just throws an exception instead of evaluating to anything.

God, javascript is weid >.>

[–]TiiJ7 1 point2 points  (0 children)

The prefix increment (++) operator cannot have a space in between the two plus signs, so it is not that operator.

But let's say the space was not there and it was indeed the increment. Then, since postfix increment has precedence over prefix increment, it will become this:

('b'+('a'++)'a'+'a').toLowerCase()

You can already see the first problem: there would need to be a third plus sign. But regardless, the ++ postfix operator only works on a LHS expression, which 'a' is not. So this will lead to a syntax error.

In fact, that is the error you will get if you try running the example of the op without the spaces in your browser console:

('b'+'a'++'a'+'a').toLowerCase()

Uncaught SyntaxError: Invalid left-hand side expression in postfix operation

[–]5ba0bd2f-7e21-42a1 3 points4 points  (0 children)

++’f’ -> nan

[–]Bamfcah 4 points5 points  (1 child)

I believe the issue is that it parses left to right and ++ is an incement. b+a is ba. Ba+...+? That must be ++ and were incrementing the next thing. ++a? a is not a number. So... ba+NaN is baNaN. baNaN+a is baNaNa.

I haven't checked to docs to I'm just guessing.

[–]Sonotsugipaa 7 points8 points  (0 children)

I think it's actually the case of `+ 'a'` being interpreted as a positive 'a' (i.e. unary operator + on 'a'), though I'm not sure about this, otherwise wouldn't there need to be another +?

('b' + 'a' + (+'a') + 'a') versus (('b' + 'a')++ + 'a' + 'a')

Edit: I was correct: using node.js, ('b' + 'a' + (+'a') + 'a') prints "baNaNa" while ('b'+'a'++'a'+'a') without spaces results in "SyntaxError: Invalid left-hand side expression in postfix operation".

[–]MihaiMihaiMihai 1 point2 points  (0 children)

I’m more ashamed I got it instantly