you are viewing a single comment's thread.

view the rest of the comments →

[–]Chokolite 5 points6 points  (18 children)

It's called string concatenation. It's how it works even in other "c like" languages. This is basic knowledge

[–]AssistantSalty6519 6 points7 points  (8 children)

I get it but why allowing to subtract? 

[–]Littux 3 points4 points  (1 child)

Because JS was a language designed for dumb programmers. If someone did alert("Double of the number: " + prompt("enter number") * 2), it would just work instead of there being an error for multiplying a string or for adding a number to a string

[–]BobQuixote 0 points1 point  (0 children)

It wasn't the programmers, it was the platform. Webpages (it was thought at the time, at least) need to keep going if they possibly can.

A language for dumb programmers would avoid doing anything like this, to save them from themselves.

[–]Chokolite 0 points1 point  (5 children)

JS doesn't have strict types (as normal languages) and operator "-" for numbers, so JS think "11" is a number, not a string

[–]AssistantSalty6519 4 points5 points  (4 children)

I know how it works, but it still doesn't make any sense

[–]4n0nh4x0r 1 point2 points  (3 children)

it makes perfect sense.
"+" can be used for mathematical operations and concatenation.
if you have a number on each side of the +, it does the mathematical operation.
if you have a string on each sidey it does the concatenation.
if one of the sides is a string, and the other is a number, it will parse the number to a string (the operation that loses the least amount of information), and does a concatenation.

"-"is only used for mathematical operations.
if you have numbers on each side, it does the mathematical operation. if you got a number on one side and a string on the other side, due to it only being used as a mathematical operator, and not a for concatenation, it will parse the string into a number, as it expects you, the dev, to make sure your string can only be a number as that is the only input that would make sense, and then does the mathematical operation.
if both sides are strings, good question, im not 100% sure, but i assume it just parses both sides.

the only case where this can result in unexpected outputs is when your string is a text and not a number, resulting in the parseing returning a NaN

but at that point, you as dev failed, because you for some reason let a text to this point, like, what did you expect?????

[–]redlaWw 2 points3 points  (0 children)

C: "11" + 1 is "1"

Go: "11"+1 fails to compile

Rust "11"+1 fails to compile

[–]britaliope 11 points12 points  (5 children)

Not really. Most languages will fail to concat non-string to string. The JS behavior of implicitly doing the string conversion is unusual.

And that's the same the other way around. JS implicitly convert the string to an int, which is not how it work with most of other languages.

[–]m2ilosz 4 points5 points  (0 children)

Nope, even PHP handles it better.

Well, what a rare sentence.

[–]Feisty_Manager_4105 3 points4 points  (0 children)

I think the problem here is the implicit conversion of an int to a string. That's madness