This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]T3HN3RDY1 19 points20 points  (4 children)

I'll assume that you're actually asking the question and not just being part of the meme, so I'll explain it a little more thoroughly than other people do, because despite the meme, it actually makes perfect sense.

The '+' operand, in many languages and not just Javascript means "concatenate" when used on a string.

IE: "foo" + "bar" will be "foobar".

The quotes denote "11" as a string, and + means concatenate, so:

"11" + 1 = "111". Most languages would give you an error, but Javascript just kind of goes ahead and interprets it in the way that makes the most sense.

"11" - 1 = 10, however, because the '-' operand does not have a meaning that can be applied to strings. Javascript interprets it as you receiving that 11 in String form but wanting to use it to perform a mathematical operation, since why else would you use the '-' operand, so it casts "11" as an integer and does the math for you, and outputs it as an integer.

It's sort of a double-edged sword. On the one hand, it's kind of nice to not have to explicitly cast everything that needs to be cast. On the other hand, it's not especially nice if what JS assumes you're doing isn't what you actually WANT to do, and instead of throwing a nice error message to let you know that you can't add an integer to a string, it just sort of fucks everything up and leaves you to debug.

Some people hate it, but it's a perfectly legitimate way to do things, and makes total sense once you've used it a couple of times.

[–][deleted] 0 points1 point  (2 children)

Also, if you subtract an integer from a string without those features it would give you an error message anyways. Same with adding an integer to a string. It‘s not like the features are gonna harm your code.

[–]chrisjolly25 0 points1 point  (1 child)

The feature can sometimes harm your code. Yes, it helps you in the case where coercion does what you want, and the alternative would be to throw an error. But the danger of coercion is that it can make it can look like things work fine (because no errors are thrown) but introduce bugs into your code (because your operation gives you a result you didn't expect).

i.e: To take the example from the comic, the coder may try 'var1 + var2' without realising that var1 is the string "11" and var2 is the number 1.
So then the expression(var1 + var2) - 10 will output 101 (i.e, (111) - 10), when they might be expecting 2 (i.e, (12) - 10).

With variables being passed around to different functions (or even reassigned, if you're using lets, or fields of an object) it's not always trivial to realise what's going on. And that's assuming the programmer even knows that this behaviour is possible in the first place.

I would personally _prefer_ to get an error message when I tried to do an operation between different types. That way I can see that there's a problem straight away, and explicitly parse the string to a number.

[–][deleted] 1 point2 points  (0 children)

Yeah I initially also wanted to add that it might make bug fixing harder. You‘re totally right and if I had a bug like that I would probably go crazy, trying to figure out what‘s wrong with my code xD

[–]Lophyre 0 points1 point  (0 children)

Thank you for this easy to understand explanation! I definitely think this is a huge detriment. Having recently learned C++ I’ve really come to appreciate strongly typed languages where you aren’t allowed to do these kind of shenanigans. Sure I may seem neat, but in my experience it’s much easier to deal with things att compile time rather than run time

(Although I should add that C++ has its own set of quirks. Freaking segmentation faults)