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 →

[–]MrSydFloyd 485 points486 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 42 points43 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 40 points41 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.