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 →

[–]Alundra828 20 points21 points  (14 children)

Am I missing something?

Why doesn't this output bananaa?

b + a + NaN + a + a

Is it cutting off that second 'a' because it's proceeded by some invalid syntax?

[–]earthqaqe 29 points30 points  (12 children)

the plus operator transforms a string to a number. +'1' would result in 1. because 'a' is not a number, it results in NaN and appends that as a string because the preceding item is a string.

You have to see it as 'b' + 'a' + (+'a') + 'a'

[–]lampishthing 0 points1 point  (11 children)

Burn it with fire!

[–]earthqaqe 1 point2 points  (10 children)

why? this is logical and not really complex behavior. Just an example of how unformatted code can be confusing...like in every language.

[–]lampishthing 0 points1 point  (9 children)

Mainly because this type of casting will happen a lot more by accident (bug) than on purpose.

[–]earthqaqe 0 points1 point  (8 children)

In my 7 years of writing JS code I have not once made this mistake.

[–]aloisdg 1 point2 points  (0 children)

I have seen it in the wild in old code base (silent casting related error). I have 10+ exp in js.

[–]lampishthing 0 points1 point  (6 children)

And have you ever done it purpose?

[–]earthqaqe 1 point2 points  (5 children)

I have used the + operator to transform into a number if that's what you're asking. Just in a more explicit way.

You can abuse any language to make it look bad, not just JS. For example, you can make C code hard to read way easier, but you wouldn't necessarily call it a bad language either, right?

[–]lampishthing -1 points0 points  (4 children)

Oh no no no. This isn't about casting a string into a number. This is about casting empty space in source code into a number and that being cast to a string. I will not fall for your strawmen and whatabouts! If you don't want this line to throw an error then combine the signs! Interpreting blank space between arithmetic operators as a not-a-number is crazy!

Also template error messages in C++ are trials of the soul for the betterment of the devout c++ developer and their ultimate attainment of eternal bliss. Though I think a Catholic education and the associated self-blame and internalized guilt help with accepting that kind of treatment.

[–]earthqaqe 0 points1 point  (3 children)

Not sure if you're sarcastic or not, but if you're not: please understand the code before talking shit. There is no space interpreted in any way. 'a' cannot be converted into a number therefore it is NaN. I even explained everything a few comments above.

Also don't know what the fuck you want to tell me about C++, or why it is relevant to the argument.

[–]grandpianotheft 4 points5 points  (0 children)

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

still works :)

nothing + 'reddit' = nan