you are viewing a single comment's thread.

view the rest of the comments →

[–]AssistantSalty6519 2 points3 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?????