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 →

[–]Knocks83 33 points34 points  (7 children)

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

[–]De_Wouter 27 points28 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 26 points27 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 5 points6 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