▲ 1109 ▼ All of IT in one image (lemmy.ml) submitted 1 year ago by cm0002@lemmy.world to c/programmer_humor@programming.dev 52 comments fedilink hide all child comments
[–] squaresinger@lemmy.world 4 points 1 year ago (1 child) "a"+"b" -> "ab" "a"-"b" -> NaN permalink fedilink source parent hideshow 2 child comments replies: [–] marcos@lemmy.world 2 points 1 year ago (1 child) Yeah: parseInt("a") -> NoT a NuMbEr permalink fedilink source parent hideshow 2 child comments replies: [–] squaresinger@lemmy.world 2 points 1 year ago Sure, but the main issue here is that JS doesn't only auto cast to more generic but in both directions. Maybe a better example is this: "a" + 1 -> "a1" "a" - 1 -> NaN With + it casts to the more generic string type and then executes the overloaded + as a string concatenation. But with - it doesn't throw an exception (e.g. something like "Method not implemented"), but instead casts to the more specific number type, and "a" becomes a NaN, and NaN - 1 becomes NaN as well. There's no situation where "a" - "b" makes any sense or could be regarded as intentional, so it should just throw an error. String minus number also only makes sense in very specific cases (specifically, the string being a number), so also here I'd expect an error. If the programmer really wants to subtract one number from another and one or both of them are of type string, then the programmer should convert to number manually, e.g. using parseInt("1") - parseInt("2"). permalink fedilink source parent
[–] marcos@lemmy.world 2 points 1 year ago (1 child) Yeah: parseInt("a") -> NoT a NuMbEr permalink fedilink source parent hideshow 2 child comments replies: [–] squaresinger@lemmy.world 2 points 1 year ago Sure, but the main issue here is that JS doesn't only auto cast to more generic but in both directions. Maybe a better example is this: "a" + 1 -> "a1" "a" - 1 -> NaN With + it casts to the more generic string type and then executes the overloaded + as a string concatenation. But with - it doesn't throw an exception (e.g. something like "Method not implemented"), but instead casts to the more specific number type, and "a" becomes a NaN, and NaN - 1 becomes NaN as well. There's no situation where "a" - "b" makes any sense or could be regarded as intentional, so it should just throw an error. String minus number also only makes sense in very specific cases (specifically, the string being a number), so also here I'd expect an error. If the programmer really wants to subtract one number from another and one or both of them are of type string, then the programmer should convert to number manually, e.g. using parseInt("1") - parseInt("2"). permalink fedilink source parent
[–] squaresinger@lemmy.world 2 points 1 year ago Sure, but the main issue here is that JS doesn't only auto cast to more generic but in both directions. Maybe a better example is this: "a" + 1 -> "a1" "a" - 1 -> NaN With + it casts to the more generic string type and then executes the overloaded + as a string concatenation. But with - it doesn't throw an exception (e.g. something like "Method not implemented"), but instead casts to the more specific number type, and "a" becomes a NaN, and NaN - 1 becomes NaN as well. There's no situation where "a" - "b" makes any sense or could be regarded as intentional, so it should just throw an error. String minus number also only makes sense in very specific cases (specifically, the string being a number), so also here I'd expect an error. If the programmer really wants to subtract one number from another and one or both of them are of type string, then the programmer should convert to number manually, e.g. using parseInt("1") - parseInt("2"). permalink fedilink source parent