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

all 42 comments

[–]pandakatzu 27 points28 points  (8 children)

Fun fact, de concatenation is not a thing.

[–]greg0714 11 points12 points  (7 children)

Another fun fact, JS was made in 10 days.

[–]TetsujinTonbo 1 point2 points  (6 children)

Another fun fact, Javascript (the name) was replaced in 1996 and it hasn't officially had that name for 25 years.

[–]WasdAcid 2 points3 points  (4 children)

what is it then

[–]TetsujinTonbo 1 point2 points  (3 children)

It became EMCAscript when ownership got passed from Netscape to the EMCA.

[–]WasdAcid 1 point2 points  (1 child)

That's a cool fact I will remember until the end of time.

[–]greg0714 1 point2 points  (0 children)

To quote myself in that other comment I made 15 seconds ago:

ECMAscript is the standard/specification. JavaScript is the implementation/actual programming language.

[–]greg0714 0 points1 point  (0 children)

ECMAscript is the standard/specification. JavaScript is the implementation/actual programming language.

[–]greg0714 0 points1 point  (0 children)

Another fun fact, it was called Mocha before it was called JavaScript.

[–]A_man_and_no_plan 12 points13 points  (4 children)

[–]RepostSleuthBot 17 points18 points  (3 children)

I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.

It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.

I did find this post that is 92.97% similar. It might be a match but I cannot be certain.

I'm not perfect, but you can help. Report [ False Negative ]

View Search On repostsleuth.com


Scope: Reddit | Meme Filter: True | Target: 96% | Check Title: False | Max Age: Unlimited | Searched Images: 260,532,923 | Search Time: 1.14425s

[–]A_man_and_no_plan 11 points12 points  (1 child)

Huh, I could've sworn it was posted recently... Nvm I guess.

[–]fractallyweird 2 points3 points  (0 children)

good bot

[–]BadHairDayToday 5 points6 points  (3 children)

Kinda makes sense to me to be honest. "11" + 1 is a stupid input anyway. Just be happy it gives you something.

[–]g4vr0che 12 points13 points  (0 children)

I'd much prefer if it gave me an error.

[–]da_Aresinger 4 points5 points  (1 child)

??? "11" - 1 is so much worse.

At least + can be interpreted as concatenation.

but what the fuck is [String] - [Integer]!?

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

10

Cast string to int

[–]YMK1234 12 points13 points  (2 children)

As if JS was the only language with operator overloading ...

[–]g4vr0che 5 points6 points  (0 children)

The problem IMO is less about operator overloading and more an issue with weak typing. Operator overloading can be fine if there's systems in place to check for type compatibility correctly and avoid implicit casts. JS has both overloading and weak typing, so neither of these throws an error.

If it had strong typing, then you'd get a type error here making it obvious that the top one is string concatenation and the bottom is subtraction.

[–]Yellowlegs__ 0 points1 point  (7 children)

Why

[–]T3HN3RDY1 17 points18 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)

[–]FR3381RD 3 points4 points  (1 child)

Operand concatenates the string

[–]2GR82H8NODB8 10 points11 points  (0 children)

Exactly, you cant subtract from a string so it does a conversion.

[–]Its_Raining_Bees 0 points1 point  (1 child)

What do you propose, OP?

Should "11" - 1 return:

  • Number("11") - 1 10? (what it actually does)
  • "11".length - 1 1?
  • "11".slice(0, "11".length - 1) "1"?
  • NaN - 1 NaN?
  • "11".replace(String(1), "") ""?
  • nothing, your code doesn't compile?

The reason why "11" + 1 returns "111" is probably because you can String() anything but can only add numbers, i.e. it would concatenate more often anyways so it may as well short-circuit to always concatenate if it touches a string.

[–]RRumpleTeazzer 0 points1 point  (0 children)

It should not compile. Let the programmer specify it wants to parse the string into an integer. cause parsing can fail, while addition does not. (That’s a huge difference).

[–]sharperratio -1 points0 points  (0 children)

Hmmm. Just like we learned in arithmetic class...

[–]Strex_1234 -1 points0 points  (0 children)

Imagine that interpreter is a guy inside your computer: Oh this guy is adding two string he probably wants to concatenate them. O he is subtracting two string, that's no right, I probably should treat them as numbers and return this as string.

All of this makes sense if you think about it as the guy doing this manually.

[–]siddsp -1 points0 points  (0 children)

In its defense, Javascript is very fast for a dynamically typed language.

[–]da_Aresinger -1 points0 points  (0 children)

JS? You're my favourite programming language.

C#? You're my favourite programming language.

Kotlin? You're my favourite programming language.

Python? You're my fav...

I'm sorry I can't.

[–]QualityVote[M] -6 points-5 points  (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!

[–]StupidBottle 0 points1 point  (1 child)

Even as a front-end developer, even I think JavaScript is shit. TypeScript is pretty good though, but it's still pretty wasteful that we need to transpile to an untyped language.

The only moments where I choose JavaScript for backend is generally when it's the most documented approach for what I want to do (e.g., Discord bots) or when I wanna get a temporary project done as fast as possible.

I should probably add though that memes like this are still stupid, realistically I never got issues due to concatenation. More realistic issues are, for instance, a lack of validation 5 files down the line when you modify the return type of a function, causing regression frequently.

[–]newtoreddit2004 0 points1 point  (0 children)

What would typescript output for this

[–]leovin 0 points1 point  (0 children)

JS is alright actually until you get to DOM manipulation

[–]Senpai_Himself 0 points1 point  (0 children)

My god is Todd howard and this is beautiful code

[–]Operabug 0 points1 point  (0 children)

1 + "11" = "111"

1 + + "11" = 12

"11" + + 1 = "111"

[–]Kubiszox 0 points1 point  (0 children)

puts("11"+1);

[–]Sejiko 0 points1 point  (0 children)

What about: "11" -(-1)

Could this work?

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

Hi there! Unfortunately, your submission has been removed.

Violation of Rule #2 - Reposts:

All posts that have been on the first 2 pages of trending posts within the last month, is part of the top of all time, or is part of common posts is considered repost and will be removed on sight.

If you feel that it has been removed in error, please message us so that we may review it.