Comment

Comment by rprospero

You're looking at the wrong side of it. The subtraction does behave in a perfectly logical manner. The problem is that, given the the behavior of the subtraction operator, the addition operator's actions are illogical. Specifically, I'd argue it's perverse because it breaks commutativity:

'5' + 3 - 3 != '5' - 3 + 3

The logical approach would be to only assume that '+' is a string concatenation if both operands are strings and otherwise type coerce into numbers. Then:

'5' + 3 = 8 '5' - 3 = 2

To someone who doesn't code Javascript for a living, the above seems like a far more consistent and useful behavior.

parent rprospero 15 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton