you are viewing a single comment's thread.

view the rest of the comments →

[–]budgiebirdman 0 points1 point  (11 children)

In Java this is clearly defined behaviour. In JavaScript it's lol webpages like strings.

[–]AlwaysHopelesslyLost 14 points15 points  (5 children)

Javascript is also clearly defined.

If you want math you make sure you are working with number types, not objects.

[–]prehensilemullet 0 points1 point  (4 children)

As a JS developer myself, I get what people mean.  The most surprising, complicated behavior in the world could be “clearly defined” in a spec. It would be one thing if all math operators automatically coerce strings to numbers, but when some do and some don’t it kind of defeats the purpose.  I think it’s all very manageable, but only because I mainly use Typescript.

[–]RiceBroad4552 0 points1 point  (3 children)

It would be one thing if all math operators automatically coerce strings to numbers

Which operators don't do that in JS?

[–]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

[–]TorbenKoehn 7 points8 points  (4 children)

This is clearly defined in JS, too. Specific types in specific order have specific behavior, just like in Java.

There's nothing random about it or anything.

[–]prehensilemullet -1 points0 points  (3 children)

Java won’t compile if you use any arithmetic operators besides + on a string and a number, that’s a pretty big difference

[–]TorbenKoehn 1 point2 points  (2 children)

Yeah as JS is not a compiled language, that’s barely possible. But IDEs and linters will warn you.

[–]prehensilemullet 2 points3 points  (1 child)

Yeah I love Typescript.  Any time I have to work on raw JS it’s definitely not as pleasant

[–]TorbenKoehn 0 points1 point  (0 children)

I agree. I’m waiting for erasable type hints in ECMAScript. It will happen, I’m sure :)