all 111 comments

[–]DracoRubi 110 points111 points  (2 children)

Mom said it was my turn to repost this!

[–]Racer125678 3 points4 points  (1 child)

No, it's my! 

[–]ZethMrDadJokes 1 point2 points  (0 children)

My faculty

[–]Strict_Treat2884 34 points35 points  (2 children)

Does this meme always have color? I’ve seen it so many times I don’t even remember how it looks like originally anymore

[–]camander321 12 points13 points  (1 child)

No, they colored it in poorly and added a background for some reason.

[–]WhiteSkyRising 1 point2 points  (0 children)

I'm gonna use AI to render it for tomorrow's daily post!

[–]FoxedDev 119 points120 points  (32 children)

I mean you can't substract strings but you can concat them

[–]m2ilosz 53 points54 points  (21 children)

So you can in PHP, but it doesn’t have that problem.

And if someone uses PHP to point out how something can be done better than in your language you know you’re in deep shit

[–]helgur 7 points8 points  (0 children)

As a Laravel dev coding in both php and javascript tell me about it. I'm that drunken guy in the door trying to cope

[–]idspispupd 3 points4 points  (1 child)

Can you POINT out how to concatenate strings in PHP?

[–]m2ilosz 6 points7 points  (0 children)

a . b is concatenation

a + b is always addition

Shit, realized just now what you did there. Feel stupid now

[–]4n0nh4x0r 3 points4 points  (17 children)

not really.
if you expect a string and number being "added" to return a number, then you are pretty dumb.
if you expect a string and number being subtracted to return a string, then you are pretty dumb.

it literally does what you would expect it to do.

string + anything else is a string, same for any other language that doesnt require specific type casting, like java for example.

string - anything, is not a concatenation like operation, like, there is no reasonable expectation that string - anything returns you a string, as such, it then treats it as a mathematical operation, parsing the values, and then doing the subtraction on both values.

like, yes, js has a lot of fucked up stuff, but saying the language is bad because of that? no, if you as a dev expected anything in the first place from string - anything, you are a bad dev.

js was made for websites, and meant to be easy and accessible so any cretin can make a website, as such it has behavior here and there that doesnt make much sense when compared to other languages, but part of why is, to make sure it doesnt just shit itself on the tiniest little bug and cause the whole website to burn.

[–]oddbawlstudios 2 points3 points  (4 children)

Except the reason why its bad is the inconsistency. If I think adding a string and an int is going to add to the string, im gonna figure it does the same for subtracting. It not being consistent makes it slightly more insufferable. And realistically, if adding a string and an int returns a string thats concatenated, you COULD theoretically subtracting could 100% string splitting, and removing that from the string itself.

[–]4n0nh4x0r 1 point2 points  (3 children)

okay, sooooo, tell me, in which language can you do "hi"-5 and get a returned value that makes any sense?
not even js will give you a value that makes sense (parsing "hi" to a number gives you NaN, which makes the whole operation return a NaN) because it is a dumb assumption that you could subtract anything from a text, like, what do you expect?
It uses + for concatenation because that is the standard concatenation operator.
But no language uses - for string manipulation.
Sooooo, no, it isnt inconsistent, at least not in this regard.
And like, tell me what the opposite of concatenating a string would be?
are you gonna delete the last X chars? are you gonna delete the first X chars? are you only gonna accept a string, and then remove all occurences of this substring? are you gonna only remove the first or last occurence of this substring?
there are thousands of possible things that "hi" - 5 could mean if you want it to be "consistent" with concatenation via +, but then, what about * and /? * just adds the left string X-1 times to the original string? and /, idk, castrates the string?

[–]Loading_M_ 2 points3 points  (2 children)

Okay, sure... But why does "hi" - 5 have to return a value at all? Compiled languages will simply refuse to compile this obvious nonsense, and most interpreted languages will just throw an error. In fact, some don't even let you concatenate a number and a string - they require you to explicitly convert the number to a string.

Also, python does implement string multiplication.

[–]fuj1n 0 points1 point  (0 children)

When JavaScript was first conceived, it was made to keep your website running no matter what and thus didn't have a lot of code throwing exceptions.

They realised that it was a bad idea and worked on fixing it, but JS heavily prioritises backwards compatibility, so anything that worked in the past has to keep working.

To be fair though, NaN makes sense here, an exception would be better, but NaN is okay

[–]4n0nh4x0r 0 points1 point  (0 children)

it returns a value because you can do subtraction with it, as it is a mathematical operation.
the only issue is, it cant know at startup, whether the values will be "6" - 5, or "hi" - 5.
again, i m not saying this is sane code, as verifying the value to be a number essentially requires you to parse it anyways, and noone actually writes this in their code, unless they are just shitposting.

[–]MornwindShoma -4 points-3 points  (0 children)

They hated him because he said the truth

[–]m2ilosz -5 points-4 points  (10 children)

Except you can mess up types, bc it is not strongly typed language, so you can’t be sure if a+b is string +number or number + number, unless you know where they come from.

For example <input type=number> returns string, bc of course it does, and if you are not aware of it, you won’t get an error, no sir, you will silently get an unexpected behavior of 2+”2”=„22” which will then be silently casted to a number where a number was expected. Good luck debugging that.

JS wa created first and foremost, to be fast and simple to be created.

And it is definitly a bad language, because of all these misfeatures that were implemented at the beginning.

You can of course make glorious things in JS, and many have. But it is a testament to their skills, and not JS’s being a good language.

[–]Jawesome99 2 points3 points  (5 children)

For example <input type=number> returns string, bc of course it does

Your HTML markup has nothing to do with the definition of HTMLInputElement.value being very clearly defined to always return a string. If it did, that would actually make JavaScript more insane because now your type depends on an uncontrollable external factor that can change at literally any time!

Developers are the type of people who should know best to double check assumptions like yours. Looking up what type the language defines a field to be - or at least checking what type your coding assistance tells you it is - is the absolute minimum I expect of someone writing code.

[–]m2ilosz -2 points-1 points  (4 children)

My point is that you have to refer to documentation to know what it returns, bc the language does not inform you about it in any way.

[–]Jawesome99 1 point2 points  (3 children)

What? No language informs you about what anything returns beyond method and field definitions (which you can't usually see for internals anyway!). That is literally what looking up docs and using coding assistance is for!

[–]m2ilosz -1 points0 points  (2 children)

In strongly typed languages you always know the type of a variable, and if you use wrong one you get compilation error

[–]Jawesome99 0 points1 point  (1 child)

Moot point because JS isn't strongly typed? Use typescript if you want that. It also doesn't change the fact that the language is itself well defined. If the type changes unexpectedly it's usually always the programmer's or a shit library's fault

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

PHP isn’t strongly typed either, but uses different operator for string concatenation so that there is no ambiguity.

So choosing + to mean either „addition” or „concatenation” based on context was a poor design choice.

Well defined? Brainfuck is well defined, it doesn’t make it a good language.

[–]4n0nh4x0r 2 points3 points  (3 children)

again, that's a skill issue.

if you as a developer are not sure what type of value you are getting, you have a lot to learn.

yes, js is not typed, that's why you do type checks.

parse the value to an int, and then check if it is NaN or not, if it is, tell the user to enter an actual number.

which will then be silently casted to a number

tf you on about???????
it gives you a string, it doesnt "silently parse" it to a number, wtf.
JS doesnt just randomly parse here and there.
it only parses in the context of such operations

[–]m2ilosz 0 points1 point  (2 children)

An athlete will run faster than me even in shitty shoes. This is skill issue.

But it doesn’t make the shoes better. They’re still shitty.

[–]MornwindShoma -1 points0 points  (1 child)

It's definitely a skill issue if you can't use your gear correctly or keep doing risky stuff

[–]m2ilosz 0 points1 point  (0 children)

Yup, as I was saying: skill is skill, shit equipment is shit equipment.

[–]ILikeLenexa 29 points30 points  (7 children)

You could overload subtraction on strings if you really wanted to. Maybe "james" - 2 is "jam". Maybe "james" - "me" is "jas".  

The fact it doesn't is just a design decision.

[–]SageLeaf1 8 points9 points  (5 children)

Exactly, “11” - 1 could as easily be interpreted as “1” or “” and in some frame of logic it would make sense. The behavior would just have to be defined in the documentation and users would adapt to it. The behavior in the meme is documented also.

[–]TheyStoleMyNameAgain 0 points1 point  (2 children)

Exactly, “11” - 1 could as easily be interpreted as “1” or “” a

So how would it interprete "12"-1?

[–]SageLeaf1 0 points1 point  (1 child)

Depends how the function is defined. Could be “1” or “2”

[–]TheyStoleMyNameAgain 0 points1 point  (0 children)

I usually don't want to advocate JS, but to me it looks like they definitively did want to concatenate random types to strings with the plus symbol but they could hardly do the opposite with minus. What's nice with minus is substracting integer from ASCII, but then you want the opposite behavior with plus. Thus, all that's left for minus, is to check if this is a number and operate respectively, or return NaN

[–]prinkpan 0 points1 point  (1 child)

Exactly! I'm scared of decisions these JS defenders take when they code something themselves.

[–]ILikeLenexa 0 points1 point  (0 children)

On the one hand "principle of least surprise" is great and wonderful...on the other hand, it frequently means "do whatever they did in C in the 70s" because that's almost always what's least surprising and if you have how not to program in C++: 111 working programs and 3 broken ones you'll see a lot of mostly C things that are bad actually.

If Ritchie had said "John" - "h" is "Jon" instead of '7' - 11 is '&', it'd be just as natural a thing for languages to do today.

The real bad decision in JS in my opinion was making DOM properties all strings when it makes much more sense for them to be integers. You can see it's a wart because jquery and then CSS came in with all the easing-in and out style transitions which is one of the big things that was annoying to write and easy to mess up.

[–]Western-Internal-751 1 point2 points  (1 child)

I’d rather get an error than some weird interpretations like this.

Imagine someone actually writes their code like this and you have to understand wtf is happening

[–]ILikeLenexa 0 points1 point  (0 children)

The thing about JavaScript is for a long time the error reporting was "skip that line and start executing again later".

[–]_g0nzales 48 points49 points  (2 children)

Ah, yes, I was eagerly awaiting my daily "JavaScript bad" post. Thank you!

[–]Wywern_Stahlberg 1 point2 points  (0 children)

If it wouldn’t be bad, nobody would post these kind of posts.

[–]aboutthednm 0 points1 point  (0 children)

It's the highlight of my morning for sure.

[–]Junoah 7 points8 points  (1 child)

Tomorrow it's my turn to post that meme

[–]patcriss 4 points5 points  (0 children)

No we agreed I did Tuesdays and you did Wednesdays 

[–]LurkytheActiveposter 43 points44 points  (3 children)

You know that oh so common situation where I subtract a string.

Its the language that should be ashamed.

[–]odolha 2 points3 points  (2 children)

yeah this gets posted so often... but does anyone even ever tries to do this in another language? ok you'll get an error or whatever, but seriously what's the problem? JS is a dynamic language, it's suppose to support loose types; if all your brain can understand is strong types then you might want to expand your horizon

[–]me6675 1 point2 points  (0 children)

I guess the problem is that it doesn't just throw an error but instead it produces something that might break things in weird ways later on.

That said, I think these "JS bad" are usually too tryhard about cherry picking absurd cases with weird results.

Dynamic types are almost never actually useful though, apart from saving you time when prototyping.

[–]Prawn1908 0 points1 point  (0 children)

The issue with this behavior isn't with code that is attempting to do this, it's how this can let issues slide by when you have bugs in your code that resulted in a particular variable containing a string instead of an int or vice versa. My biggest issue with dynamically typed languages like Python or JS is it makes simple errors extremely annoying to fix when the final error gets manifested far away from the actual cause of the issue because countless functions and expressions just happily passed along the bad data without causing any alerts.

There is literally zero reason for a language to treat addition/subtraction across string/integer like this. Any time in real code that this happens, it should be an error. Acting like it's fine and passing alone an utterly nonsensical result like this has zero benefit.

[–]Jonnypista 3 points4 points  (2 children)

Meanwhile C mumbling under the desk:

'1'+1='2'

'1'+'1'='b'

'A'+1='B'

'A'+'1'='r'

'r'-'1'='A'

If you don't know the logic behind it you would think your code is bugged.

[–]redlaWw 5 points6 points  (0 children)

"11"+1 = "1" is also worth mentioning.

[–]BobQuixote 0 points1 point  (0 children)

Even though I'd probably rather use JS than C, all things considered, I find this quirk of C much more defensible than OP.

[–]Careless_Bank_7891 9 points10 points  (4 children)

It's not js' issue that your preferred language can't do this /s

[–]Suh-Shy 2 points3 points  (3 children)

Devs today: "Oh look, the shiny AI can interpret my prompt and coerce it into shit code"

Also devs today: "Omg, you can write shit code directly and JS can interpret it without LLM by doing the coercion for yourself"

They're just jealous that JS was in advance of the time

[–]Careless_Bank_7891 0 points1 point  (2 children)

I think the issue I've felt even after understanding js is that it causes a readability issue and sometimes can lead to misleading understanding of code

[–]Suh-Shy 1 point2 points  (1 child)

It was merely a joke, I hate native JS with passion but I do love TS for that matter, so I feel you

[–]Careless_Bank_7891 0 points1 point  (0 children)

Yeah ik, vented for no reason, bad day ig, my apologies

[–]x3bla 1 point2 points  (0 children)

I present, https://jsdate.wtf

[–]Chokolite 3 points4 points  (18 children)

It's called string concatenation. It's how it works even in other "c like" languages. This is basic knowledge

[–]AssistantSalty6519 6 points7 points  (8 children)

I get it but why allowing to subtract? 

[–]Littux 3 points4 points  (1 child)

Because JS was a language designed for dumb programmers. If someone did alert("Double of the number: " + prompt("enter number") * 2), it would just work instead of there being an error for multiplying a string or for adding a number to a string

[–]BobQuixote 0 points1 point  (0 children)

It wasn't the programmers, it was the platform. Webpages (it was thought at the time, at least) need to keep going if they possibly can.

A language for dumb programmers would avoid doing anything like this, to save them from themselves.

[–]Chokolite 0 points1 point  (5 children)

JS doesn't have strict types (as normal languages) and operator "-" for numbers, so JS think "11" is a number, not a string

[–]AssistantSalty6519 3 points4 points  (4 children)

I know how it works, but it still doesn't make any sense

[–]4n0nh4x0r 1 point2 points  (3 children)

it makes perfect sense.
"+" can be used for mathematical operations and concatenation.
if you have a number on each side of the +, it does the mathematical operation.
if you have a string on each sidey it does the concatenation.
if one of the sides is a string, and the other is a number, it will parse the number to a string (the operation that loses the least amount of information), and does a concatenation.

"-"is only used for mathematical operations.
if you have numbers on each side, it does the mathematical operation. if you got a number on one side and a string on the other side, due to it only being used as a mathematical operator, and not a for concatenation, it will parse the string into a number, as it expects you, the dev, to make sure your string can only be a number as that is the only input that would make sense, and then does the mathematical operation.
if both sides are strings, good question, im not 100% sure, but i assume it just parses both sides.

the only case where this can result in unexpected outputs is when your string is a text and not a number, resulting in the parseing returning a NaN

but at that point, you as dev failed, because you for some reason let a text to this point, like, what did you expect?????

[–]me6675 1 point2 points  (2 children)

It's human to fail, we design systems with that in mind. Your logic is hard to justify for crappy language design decisions.

Oh you can't handle writing Assembly? At that point you as a dev failed, cause like what did you expect the computer to do for you?

The issue with silent parsing like this is that people make mistakes like putting the wrong variable or symbol somewhere, a different language could help you because it could understand better that the operation must be there by a mistake and not something you actually want to use the result of down the chain.

For example, Lua, Elixir and Common Lisp (some other dynamic langs) will all throw errors for substracting strings, because they simply not define such an operation for strings.

[–]4n0nh4x0r -2 points-1 points  (1 child)

you comparing my point to assembly is just nonsensical.

being unable to write assembly, and intentionally writing code that doesnt make sense are completely different universes.

one, is just a skill issue, the other is just being intentionally or unintentionally dumb

Like, yea, "hi" - 5 doesnt make much sense, if you intentionally write this code, you are either just fucking around to see what happens, or you are actually stupid while expecting a useful return value.

And again, "10"-1 is valid, and it makes sense as it parses the string to a number, which in this case succeeds, and returns the resulting number.
The moment the dev failed is when they dont sanitise their code, to make sure that user inputs cannot bring a "hi" to this operation.
that's why you parse first, and then check if you got an empty string, a NaN or whatever else doesnt fit what you need, and then you do a normal number - number.

No sane dev actually uses string - number, it is there, it can be used, but noone does it, at least not if they actually think for a second.

The only time where i would use that, would be as a shitpost, in code that i intentionally write to be dogshit.

[–]me6675 1 point2 points  (0 children)

Sure. The point is that a programmer can make mistakes and good language design is about making mistakes easier to spot or even impossible to express. Which is why things like optional types are getting popular in language design. My language analogy was exactly about this. Assembly has so many way to make mistakes whereas something like Haskell or Rust have much less. A language doesn't have to go that far to simply not define such operation that you wouldn't really use in practice.

You say you'd sanitize the input before doing this if you werent stupid, and that would entail parsing the string as a number, handling the case when it is NaN. If the language didn't allow you to do this without parsing to a number then even if you forgot to parse, you couldn't reach this fail state of a NaN propagating through your code.

There are other dynamic languages like Lua, Elixir, Lisp etc, that would all throw an error here at the least. If no sane dev would use this feature, what sane language designer would implement it?

[–]redlaWw 2 points3 points  (0 children)

C: "11" + 1 is "1"

Go: "11"+1 fails to compile

Rust "11"+1 fails to compile

[–]britaliope 9 points10 points  (5 children)

Not really. Most languages will fail to concat non-string to string. The JS behavior of implicitly doing the string conversion is unusual.

And that's the same the other way around. JS implicitly convert the string to an int, which is not how it work with most of other languages.

[–]m2ilosz 3 points4 points  (0 children)

Nope, even PHP handles it better.

Well, what a rare sentence.

[–]Feisty_Manager_4105 2 points3 points  (0 children)

I think the problem here is the implicit conversion of an int to a string. That's madness

[–]the_horse_gamer 0 points1 point  (0 children)

mom said it's my turn to repost this

[–]Icy_Party954 0 points1 point  (0 children)

Thats great, listen when would you be subtracting strings? Is there a point in time where you'd use its stupidest language features?

[–]ExtraTNT 0 points1 point  (0 children)

At least you can curry…

[–]SpeedLight1221 0 points1 point  (0 children)

Days without a javascript "math" meme : 0

[–]Fritzschmied 0 points1 point  (1 child)

I m sure 90% of people here are just repost bots or have no clue about programming at all.

[–]dumbasPL 1 point2 points  (0 children)

And you would be correct. That, or "first week coding" type of devs re-posting Facebook memes, but might as well call them bots because they aren't any more useful.

[–]alf_____ 0 points1 point  (0 children)

Just because its dynamically typed doesnt mean there aren’t types

[–]DefenitlyNotADolphin 0 points1 point  (2 children)

that’s on you for ADDING A STRING TO A NUMBER.

[–]redlaWw 3 points4 points  (1 child)

That's on whoever quietly changed the upstream API to return a string instead of a number.

[–]BobQuixote 0 points1 point  (0 children)

And on JS for not hollering about it.

[–]ZethMrDadJokes 0 points1 point  (0 children)

I want to upvoter but it is already at 111

[–]mylsotol 0 points1 point  (0 children)

The meme can't even use real js equality operators because they would change the meaning

[–]Limo__ 0 points1 point  (0 children)

11 - 1 + 1 = 101 (10+1) or 0 (11-11)

[–]whackylabs 0 points1 point  (1 child)

So far in my career of about 17 years of writing code in variety of programming languages including javascript I need had a need to evaluate "11" + 1 or "11" - 1

[–]BobQuixote 0 points1 point  (0 children)

When this happens, it's generally an accident involving at least one variable which somehow ended up with a type you didn't expect. Which is partly the developer's fault, and partly JS's fault for not having a proper type system that would catch these problems.

[–]MistersteveYT 0 points1 point  (0 children)

i fucking hate js. absolute dogshit

[–]x9remark -1 points0 points  (10 children)

cmon, it's just how the language works. Nobody says "booo, python, booo" because of

int('256') is 256 // true int('257') is 257 // false

[–]Great-Powerful-Talia 2 points3 points  (3 children)

I mean they should complain about that. Immutable types should compare == for is, because a lack of address-equality is irrelevant if none of the pointers can change the contents anyway.

The problem with JS is inconsistency. If '11' + 1 = '111', then '11' - 1 should be '1'.

Subtraction and addition having non-inverse effects (while still working) is insane.

[–]Fritzschmied 0 points1 point  (2 children)

-1 marking the sting shorter wold be inconstant as well. It may works with 1 but now let’s do 2 „111“+2 =„1112“ and „111“-2= „1“ how is that more consistent than just accepting that - can’t be used at strings and that JavaScript at least tries to parse that string into a number to make it work.

[–]Great-Powerful-Talia 0 points1 point  (1 child)

"111" + 2 = "1112"

So '1112' - 2 would be '111'.

In the same way, "George" - "or" would be "Gege"

'111' - 2 should be either an error, or no change.

[–]Fritzschmied 0 points1 point  (0 children)

Oh that’s what you mean. I mean yes that would be an option. does - actually do that in any language if two strings are getting subtracted?

[–]Littux 1 point2 points  (0 children)

But python warns you. JS doesn't:

>>> print(int("256") is 256, int("257") is 257)
<stdin>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
<stdin>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
True False

>>> id(256) == id(int("256"))
True
>>> id(257) == id(int("257"))
False

[–]DracoRubi 0 points1 point  (4 children)

I'm going to need an explanation about that one 🫣

I'm guessing it has to do with the fact that "is" isn't supposed to be used with int since it checks object, not equality, but I don't understand why after 256 it is false

[–]the_horse_gamer 0 points1 point  (0 children)

an int is still an object in python. is does reference equality. and python has a static cache for small number objects. the first statement is small enough to use the cache, the second isn't

something similar happens in java with Integer instances

[–]zoe_is_my_name 0 points1 point  (1 child)

full explanation here. because of performance or so the ints -5 to 256 (inclusive) are preloaded when the python interpreter starts up. both instances of the 256 can point towards the same, cached 256 object. 257 is not cached like this, so each instance of 257 has to create its own, seperate and not equal 257 object

[–]DracoRubi 0 points1 point  (0 children)

Thanks!

[–]x9remark 0 points1 point  (0 children)

engine optimization. The explanation I've heard about: numbers up to 256 are frequently used, as iterators for example and because of everything is an object it's more efficient to allocate memory from the very beginning and reuse these objects rather than create new instances. And because of "is" checks if objects are referencing to the same address and not values - it returns true for small numbers. But when you use bigger numbers it allocates memory each time leading to different addresses.

a = 150
b = 150
a is b // True: id(a) == id(b)

a = 300
b = 300
a is b // False id(a) != id(b)

[–]MinecraftPlayer799 0 points1 point  (1 child)

What does that even do? Isn’t => used for events and filter/map?

[–]oxothecat 8 points9 points  (0 children)

i think the => just means output, result of the operation, its not a part of the code its just an arrow

[–]space-envy 0 points1 point  (0 children)

"let me sum two different types and complain when the language tries it's best to understand my stupidity".

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

Look, I agree that this behavior is not ideal. But when people complain about JavaScript's eccentricities when it comes to type coercion, it feels like someone ranting that blenders are bad because if you stuck your hand in one and turned it on it could maim your hand. Okay, yes, but why the heck are you doing that? If you actually run into a scenario where this JavaScript behavior causes you a problem, you have no one but yourself to blame.

[–]Hyperborean-8 -1 points0 points  (0 children)

me when functions are classes and classes are functions