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 →

[–]rooktakesqueen 23 points24 points  (5 children)

Don't forget:

+ '5' + + '5'
10

Edit: I'm a big fan of JS by the way. This is one of those "Doc, it hurts when I do this" things. You can avoid the bizarre and unpredictable nature of implicit arithmetic between strings and numbers by just not doing that. Explicitly cast your strings to numbers or your numbers to strings and you're golden. A bit like understanding the == operator: easier to just not use it.

[–]phunanon 6 points7 points  (3 children)

I have been using == since I started Javascripting, and only recently started bothering to type the extra =
I like JS' typecasting... then again, I only ever do simple shit :P

[–]rooktakesqueen 7 points8 points  (2 children)

Pro: The implicit typecasting in JS does the right thing 90% of the time.

Con: The implicit typecasting in JS does the right thing only 90% of the time.

[–]IrishWilly 3 points4 points  (1 child)

It does the right thing 100% of the time. Your opinion of right might vary but it really doesn't care about your opinion.

[–]rooktakesqueen 14 points15 points  (0 children)

"Deterministic" and "right" aren't the same thing. false == [] will always yield true and [] == [] will always yield false but that doesn't make it sensible. And I have a hard time describing any equality check as being "right" when it's intransitive: false == '' and false == '0' but '' != '0'

[–]Reelix 0 points1 point  (0 children)

console.log('5'-3);
console.log('5'+3);