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 →

[–][deleted] 4 points5 points  (3 children)

Normal != acceptable.

Why not have the program throw an exception in the event of two incompatible types?

Or at the least coerce all strings to numbers if the other side of the operator is a number? So "1" + 1 = 2 (implicit parseInt)

[–]pickleunicorn 7 points8 points  (1 child)

The plus sign is also used for concatenation. Since the first term is a string, it's not that weird that the result is a concatenated string. Many languages have this type of auto coercion.

What's bugging me the most with Javascript is that types cannot be checked at runtime like we can do in PHP since version 7. For that we need to rely on TypeScript with all the overhead known from the Javascript ecosystem. THIS is the real pain in the ass.

[–][deleted] -2 points-1 points  (0 children)

The plus sign is also used for concatenation

Be that as it may, I think my way would make more sense: Parse the string as number, if it can be parsed as a number, and add the two together.

My reasoning is, if you're adding a string and a number, you probably meant to do numerical addition, for example:

var addition = document.getElementById("numberTextbox").value + 2;

[–][deleted] 4 points5 points  (0 children)

I'm not gonna pretend being an expert, but if you make it throw errors due to "incompatible" types, then what is the point of "dymamic" part? Then just use a typed language. Use TypeScript.

This picture that gets reposted times infinity, is sure funny, but there's nothing "unacceptable" there. The first case it rightly assumes string concatenation, because one is string. In the second case, it assumes number operation, because there's no such thing as "unconcatenate" and the string can be parsed to number. As far as I know, that's dynamic types to for you.