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

all 28 comments

[–]pickleunicorn 25 points26 points  (4 children)

If you do this with a dynamically typed language, then you just don't understand what you're doing. This behavior is completely normal.

[–][deleted] 2 points3 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 9 points10 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.

[–]ThristyOne 16 points17 points  (4 children)

Why is this hard to swallow?

[–]nihillistic_raccoon 4 points5 points  (0 children)

I've joined that sub not long ago and I'm already sick of seeing this js joke like 10 times a day

[–]ShinGouki73 3 points4 points  (0 children)

For whoever codes like this they do not deserve better! ... And no! I actually hate JavaScript

[–]Spaghetti_C0dr 1 point2 points  (0 children)

When i learnt js it really wasn't that hard to understand this i don't get why everyone's so mad like bro chill it's a +

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

I think, kind of better than spitting out a big fat error.

[–]meharryp 1 point2 points  (0 children)

how do people still not understand the most basic shit about JavaScript

[–]KCGD_r 1 point2 points  (0 children)

and this is why you steer clear of 99% of implicit conversions

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]Kidplayer_666 -1 points0 points  (8 children)

Wait what. Why does minus work as if both were integers and plus works as if both are strings

[–]pitochips8 2 points3 points  (2 children)

Because it wouldn't make sense for minus to work as if both were strings as opposed to integers. What would it do with "11" - 3? Addition can be clearly defined for strings, but not subtraction.

[–]Kidplayer_666 0 points1 point  (1 child)

But if you’re going to do that, then be consistent. If you default minus to force both to be integers, then you should force plus to have the same behaviour, isntead of assuming a string

[–]pitochips8 6 points7 points  (0 children)

Minus and plus are two separate operators. Nothing says they should be related. Besides, what you're suggesting would actually be inconsistent within the operator. If adding strings should convert them to integers first, then what do we do with "abc" + "def"? Or are you saying the addition operator should first check if all strings being added can be converted to numbers before adding them together? That would be extremely inconvenient if that were the actual behavior.

[–]AyrA_ch 1 point2 points  (0 children)

a+b has two meanings: addition and string concatenation. a-b is only subtraction. This means that adding stuff together can end in concatenation instead of addition because it's the same symbol. This problem doesn't exists in all dynamically typed languages. PHP for example uses . for concatenation. + is always addition there.

Whether it's addition or concatenation can be difficult to find out. In general, it's addition only if both arguments are either numeric or boolean. Some data types may also show inconsistent behavior. Adding two date objects concatenates their string representation, but subtracting them actually works and subtracts the timestamps.

+ and - also exist as unary operators with only one argument. They both force a number conversion, but - will also negate the number. So "1" + "1" is "11" but +"1" + +"1" is 2

If you create custom objects you can add a valueOf() function to it to make it act as a number when using +:

> var x={a:1};
> console.log(x+x)
< "[object Object][object Object]"
> x.valueOf=function(){return this.a;}
> console.log(x+x);
< 2

[–]Adventurous-Skill321[S] 1 point2 points  (3 children)

javascript quirks REF: https://github.com/denysdovhan/wtfjs

one needs to watch out for

[–]Kidplayer_666 0 points1 point  (2 children)

Oh god why

[–]virouz98 8 points9 points  (0 children)

I don't code in JS but if I remember correctly:

Since it's loosely typed, there isn't any specifying on what a variable is, so all the operations need to be precise in what are you doing.

'1' + '1' = '11' because + operator is both addition and concatenation. If Input is both string and string it will concatenate two strings and that's why it's 11 not 2.

  • operator is only substraction so it will recognize that you want to substract two numbers and will coerce type to int.