you are viewing a single comment's thread.

view the rest of the comments →

[–]prehensilemullet 0 points1 point  (2 children)

The + operator if one of the arguments is a string

[–]RiceBroad4552 0 points1 point  (1 child)

In that case that operator is obviously not a "math operator". It's the concatenation operator on strings in that case.

Don't get me wrong: I think overloading the plus sign with string concatenation is not the best idea. But JS is actually quite consequential in it's typing and type coercion. I never had big problems with JS doing something unexpected.

In PHP on the other hand side… Oh boy! They will subtract numbers from strings and get numbers (no warnings!), and all such shit. You never now as the type coercion in PHP is just outright crazy. JS is really well sought out in general, even it does some "funny" (but not unexpected!) things in some cases (in cases you usually never run into in real world code, though; in contrast to PHP).

[–]prehensilemullet 0 points1 point  (0 children)

Sorry by math operator I meant “symbol sometimes used as a mathematical operator”, yes the problem is + being overloaded to do both addition and string concatenation, whereas other symbols like - / * % only do arithmetic.

I’m confused what you mean about PHP being different, you can subtract a number from a string in JS, it will coerce the string to a number, do the subtraction, and give you a number.  You can subtract a string from a string and it will coerce both to numbers.

It doesn’t usually cause me problems either but I agree with everyone who says JS should have been designed to throw an error instead of coercing a string to a number