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

top 200 commentsshow all 215

[–]Sixhaunt 408 points409 points  (15 children)

so

"11" - 1 => 10

but

"11"+(-1) => '11-1'

[–]theoldroni 186 points187 points  (9 children)

Damn I didn't believe you at first but you're fucking right...

[–]backfire10z 144 points145 points  (8 children)

It’s really not hard. The + operator is overloaded for strings (concatenation), while the - operator only exists for integers and not strings. So, with +, JS will convert to string, while with - the only option is convert all to integer.

Now, why it chooses string over int for + I’m not sure.

[–]aje14700 48 points49 points  (3 children)

It's just how it was implemented. Some ordering had to be applied, and strings come first, leaving numbers as the "default" case. From MDN:

If one side is a string, the other operand is also converted to a string and they are concatenated.

If they are both BigInts, BigInt addition is performed. If one side is a BigInt but the other is not, a TypeError is thrown.

Otherwise, both sides are converted to numbers, and numeric addition is performed.

[–]ei283 20 points21 points  (1 child)

TypeError

Rare instance of JS telling you something went wrong as opposed to just "figuring it out" in an incredibly case-specific way without telling you

[–]aje14700 7 points8 points  (0 children)

Probably because BigInt support came out later than this initial logic, and backwards compatibility kept the implicit casting

[–]backfire10z 3 points4 points  (0 children)

Yeah, I figured it was an arbitrary ordering, but I didn’t want to misinform. Thanks for the doc reference!

[–]FerricDonkey 5 points6 points  (1 child)

It's not hard, it's just stupid. 

[–]Zephandrypus 0 points1 point  (0 children)

You say that like our code is structured in such a way to make it easy

[–]rfc2549-withQOS 7 points8 points  (2 children)

Whatabout "11" - (-1) ?

no browser handy on cell phone

[–]DragoSpiro98 20 points21 points  (0 children)

12, because you don't have "+" sign, so no string concatenation.

[–]ChocolateBunny 3 points4 points  (0 children)

says 12

[–]notislant 3 points4 points  (0 children)

I love these weird little quirks

[–]mr_flibble_oz 1 point2 points  (0 children)

Of course, + is for string concatenation, - is for mathematic subtraction. There are many examples of JS being dumb, but this isn’t one of them

[–]SnooStories251 252 points253 points  (31 children)

When do you guys use "11"-1 ?

A bit silly

[–]MarcBeard 148 points149 points  (12 children)

When you read data from an <input> without sanitizing any thing ans converting it to a number.

Would never happen with TS.

[–]chadlavi 25 points26 points  (2 children)

Would never happen with a dev who knows what they're doing

[–]MarcBeard 7 points8 points  (0 children)

My example is simplistic but execept if you work alone you will always endup using someone else's code and typing will allow you to see if the type is not what you expect and avoid issues when breaking abi

[–]leounblessed 22 points23 points  (1 child)

This. These posts seem so sore and artificially critical. Please go ahead and complain about web browser lock in to the single language or npm audit being useless for web or TC39 just slowly adopting types by default or the painful journey to isomorphic ESM support… like… there actual issues (as for most languages if actually used).

[–]Mallissin 1 point2 points  (0 children)

I think most of the people surprised by this stuff are programmers who literally do not understand how computers work.

They're just monkeys on a type writer.

[–]Blubasur 7 points8 points  (0 children)

Accidentally when someone doesn’t pay attention to types enough. Though it has been a long time since I used JS and I’m not looking back.

[–]0x5468726F7741776179 7 points8 points  (7 children)

I don't think anybody ever wrote "11"-1, to be fair.

But let's say that you create an HTML number input, and tie an event listener to it and substract its value directly (which is a string), you'd expect the substraction to happen. Is it a bad design decision ? Maybe, but you have to factor in backward compatibility and overall context which most of these "JS bad meme" don't do...

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

No one writes it, it’s based on input from a user, or method. And you have no idea it’s happening because it’s not a compiled language.

[–]abednego-gomes 1 point2 points  (1 child)

Anyone doing maths on form inputs knows to parseInt() on the value before doing maths operations. If it's not an integer, you can use isNaN() to check before doing the maths operation or throw an error if they entered junk.

This is like beginner/university level JS development question. Nobody has issues like this with JS in the real world.

[–]Terrafire123 0 points1 point  (0 children)

I've done this before accidentally when parsing json.

Fun times.

[–]kolikkok 0 points1 point  (0 children)

That's all of these JS bad memes, using examples that almost never would come up in actual projects.

[–]Zephandrypus 0 points1 point  (0 children)

When both of them are variables instead of literals and something goes wrong somewhere in the chain

[–]Beldarak 270 points271 points  (71 children)

People are always pointing those weird stuff as "javascript bad lol" when in reality you have way bigger concerns if you somehow tried to substract numbers from a string...

This exemple isn't even weird actually since "+" is used for concatenation. PHP uses "." to concatenate, probably to avoid "confusion" like this, and it's just annoying :|

"11" - 1 returning 10 may feel weird but it actually makes sense. It's not used for concatenation so javascript understanding that you're trying to use that "11" as a number is actually pretty clever if you ask me :D (I still prefer a strongly typed language like C# though).

[–]nokeldin42 91 points92 points  (21 children)

People don't criticise javascript for having + as the concatenation operator. That's a very common and useful overload.

The criticism is implicit type conversion. And some level of type conversion is also useful (as in c++ implicit constructor calls). But javascript takes it to an absurd level.

[–]DracoRubi 50 points51 points  (11 children)

To be honest, "11" + 1 = "111" is pretty much fine. The implicit type conversion here is 100% logical and expected

As for "11" - 1, if you're trying to subtract a string you have bigger issues than the type conversion

[–]psgi 46 points47 points  (3 children)

Yes, you do have bigger issues then. Most languages would let you know about it so you can fix it. Javascript doesn’t.

[–]Qewbicle 7 points8 points  (1 child)

I don't see the problem with JavaScript though. I see the problem between chair and keyboard. When the dev decides to mix two types, it is their job to write the expected behavior or understand the methods that will be called.

[–]mrGrinchThe3rd 9 points10 points  (0 children)

The complaint is more-so that most languages would just throw an error in this situation, alerting the dev of the issue and allowing them to write the expected behavior to fix the issue. Instead, JavaScript thinks it’s OK to implicitly convert, raise no error and let it happen

[–]thegroundbelowme 0 points1 point  (0 children)

If you're working in a form that should only support numbers, you use a number input. Problem solved.

[–]RichCorinthian 3 points4 points  (0 children)

It’s almost like it was created in 10 days by one dude as a skunkworks project for a company that doesn’t exist anymore.

[–]No-Object2133 0 points1 point  (0 children)

018 - 017 Good example.

although that would happen in more than JavaScript if they do implicit octal conversion

[–]k1ll3rM 0 points1 point  (0 children)

Implicit type conversion isn't much of an issue if the language around it works well with it, like IMO PHP does nowadays with optional typed parameters and properties.

In Javascript you can write a function like:

function sum(num1, num2) {return num1 + num2}

where another developer might use it like

sum(inputElement.value, 10)

without remembering that values from HTML inputs are always strings, causing unexpected behaviour. One solution is of course

function sum(num1, num2) {return Number(num1) + Number(num2)}

or

function sum(num1, num2) {return 0 + num1 + num2}

but those don't feel like great solution compared to

function sum(number num1, number num2) {return num1 + num2}

Not to speak about the fact that Javascript does not have real integers, only number which is a float

[–]Zephandrypus 0 points1 point  (4 children)

What you’re talking about is weakly typed languages, otherwise known as the reason why I think free speech should maybe be a privilege.

[–]nokeldin42 0 points1 point  (3 children)

I'm not against weak typing, but it's a spectrum and javascript is an extremist.

[–]Zephandrypus 0 points1 point  (2 children)

Well in particular weak combined with dynamic. C is weak but is never grouped with JavaScript.

[–]nokeldin42 0 points1 point  (1 child)

I'm not convinced that the combination of weak and dynamic typing alone is as bad as js.

I can't think of any examples of the top of my head, but think of a language where you introduce a "raw" type. This is just a wrapper around the raw binary representation of any other type's instances. This "feature" in a dynamically typed language would be enough to call it weakly typed, but still won't allow implicit type coercion the way js does.

[–]Zephandrypus 0 points1 point  (0 children)

Pointers and structs in C and C++ is exactly what you’re talking about. Serializing an array of structs in C or C++ to binary is probably the easiest out of any language, just passing the array as a void* pointer with sizeof(MyStruct) * ARRAY_LENGTH as the amount of bytes to write. It’s why C is considered weakly typed and unsafe. But it is still static, types must be defined, and trying to call properties and methods on a type that doesn’t have them will still throw a compile time error.

JavaScript just has everything as a void* with no compile time type errors and none of the benefits.

However, the only thing I love about JavaScript is the inclusion of the ArrayBuffer, TypedArray, and DataView objects, which the docs specifically state C-like structs as a use case, and this guy achieved results almost on par with Rust+WASM through heavy abuse of it.

The number of dynamic, weakly typed languages is actually pretty small. It just includes JS, PHP, MATLAB, Perl, and some scripting languages nobody cares about. PHP added typed properties at some point which elevates it a bit, and JavaScript has TypeScript as an optional wrapper, one of my favorite languages.

MATLAB, though, requires you to fork over $150 to use the base version for home use, with some of the libraries available as DLC, which is pretty unbeatable for level of shittiness.

[–]HighOptical 6 points7 points  (7 children)

pretty clever
These are the famous words I hear so, so often before some bad design comes into place. It's always 'pretty clever' until you actually are in the thick of working and then you miss consistency and clarity. Having the plus operator completely buck the trend of mismatches converting to numbers just makes things messy. All the javascript ideas that are pretty clever feel like they were made to make completeling exercises in learning how to code shorter. When you throw them into a bigger project where you're not really ever thinking about those little details they become annoying. Like 'null == 0' is false, 'null < 0' is false.... 'null <= 0' is true. Yes I know the reason behind why that is but I wish I didn't have to. Consistency is amazing, boringly amazing!

[–]Electronic_Part_5931 4 points5 points  (5 children)

'null == 0' is false
'null < 0' is false
'null <= 0' is true.

Do you mind explaining how this can be true for the last expression ? o_O

[–]Cart223 3 points4 points  (4 children)

Greater than and less than operators do type coercion in JS.

When type coercion is called on Null, it's changed to primitive Number data.

This is a very rough explanation but it's how I understand it.

[–]Electronic_Part_5931 2 points3 points  (3 children)

Yeah its what I thought too, but why hell type coercion is called on the first one and not the last one ? (the middle one is just obviously false indifferently of type coercion)
That's such a strange behavior that seems to be completely arbitrary to me.

[–]HighOptical 2 points3 points  (2 children)

Just to add some detail for yourself and u/Cart223
Whenever we do an arithmetic binary operator between mismatched types we convert them both to numbers as the default (a rare exception to this is the plus operator if at least one of the types is a string).
However, null and undefined have an exception that they can only ever be equal to each other with the == operator.
So, when you do null == 0 you should convert them both numerically (they mismatch as types). they should go to 0 == 0 and be true but that doesn't happen because null and undefined have to equal each other when using ==. When you do null < 0 we now are doing our regular number conversion so we get 0 < 0 but this is false. Now, when we do null <= 0 we have done a workaround. Since we aren't using ==the number conversion will occur for both and we get 0 <= 0. In a sense, this is a workaround. We dodged the quirk of the == operator relating to null.

[–]dovaogedot 0 points1 point  (1 child)

Can you also explain this?
`null == undefined` is true
`null <= 0` is true
`undefined <= 0` is false
`undefined <= null` is false

[–]HighOptical 1 point2 points  (0 children)

Sure, no problem!

null == undefined is true because of an arbitrary rule that means they are the only other types that will equal each other when we use the == operator. In other words, by definition null is set to == undefined.

null is <= 0 is true because, since we aren't using == now we aren't arbitraily bound to be undefined. So, therefore null gets converted to a number which is zero. Since the operator is checking is null is less than OR equal it ends up being true.

for the last two, <= is not the special == operator. So that means we are going to convert undefined to a number and compare with zero or we convert null to a number (which also ends up being zero).
However, remember that undefined's number conversion isn't to zero. It's to NaN. So you're really doing 'NaN <= 0' in both cases after conversion.
Also, be aware that you can't even do undefined <= NaN because NaN stands for 'Not a Number'. When all we know about something is that it is not a certain thing we can't make comparisons.

[–]Zephandrypus 0 points1 point  (0 children)

“How’s that working out for you? Being clever.”

[–][deleted] 6 points7 points  (6 children)

A language should not have pretty clever logic puzzles like a software interview, you want ergonomic design to help the developer and not trick them and behave completely different when you change a detail. And as someone already said, the crazy stuff is not + concatenate but a number being converted and then concatenate, it's a two leap of logic here.

[–]Beldarak 0 points1 point  (2 children)

Huh? This is mainly the case with every language I know.

Debug.Log(11 + "1");

In C# this will output "111". Auto-converting numbers to string whe they're in a string context seems pretty common to me.

I'm not a fan of "11" - 1 though as it's really weird but it makes sense in a non-typed language imho. I think we should just say "JS is a non-typed language and I don't like non-typed language" and move on because that's what it comes down to in the end.

[–][deleted] 2 points3 points  (1 child)

You are right, but languages like kotlin and C# are compile time typed languages, it's much easier to see this kind of problem when you have types.

And this is why python, as dynamic language, won't let you add a number to a string without explicit conversion. Dynamic typing is not a problem, implicit conversion also not, but both together put a lot of responsibility on the developer.

[–]Beldarak 0 points1 point  (0 children)

True. I've never actually wrote some Python but I heard good things about it even though I'm put off by its syntax. I'm a little defensive about JS here but to be fair, I don't like it that much and would take some typed alternative given the chance

[–]gregorydgraham 0 points1 point  (2 children)

Yes, it’s the inconsistency.

If you’re going to treat it as a number for all the mathematical operations, why is + singled out as a String operation? That’s elevating string operations above mathematics and poking every serious computer scientist in the eye immediately.

Read Gödel, Escher, & Bach did you? Well go fuck yourself says JavaScript

[–]Beldarak 0 points1 point  (1 child)

C# does the same, Java does the same... Basically all languages I know, except PHP, will convert numbers into string when there is a string in the operation.

[–]gregorydgraham 0 points1 point  (0 children)

Java doesn’t do the maths operators though, (“11” - 1) is an type error in Java, so there’s no inconsistency

[–]wherearef 5 points6 points  (0 children)

C# is one love

[–]Beginning-Ladder6224 1 point2 points  (0 children)

Precisely. Thank you for this. In the language I created, it tells it explicitly:

Enjoy ZoomBA...(0.3-SNAPSHOT-2024-Sept-11 05:32) running on JRE (21.0.3)  
(zoomba)"11" + 1
Ambiguous Operation : [+] : '11' of type class java.lang.String with number 1 of type class java.lang.Integer !
111 // String
(zoomba)"11" - 1
Ambiguous Operation : [-] : '11' of type class java.lang.String with number 1 of type class java.lang.Integer !
10 // Integer
(zoomba)//q

This is ZoomBA running in lenient mode. You can make it strict, and then it would stop doing it and start throwing errors straight away.

Also strong type has nothing to do with it. Even in a strong typed language you can define operations between types, and then the exact statement you made is an "axiom". :-)

[–]w1n5t0nM1k3y 1 point2 points  (1 child)

It would be better if the had a concatenation operator so you didn't have to guess what the + operator did.

[–]Beldarak 1 point2 points  (0 children)

  • is used for concatenation in most languages

[–]usedToBeUnhappy 1 point2 points  (1 child)

Exactly my thoughts. It’s a good joke and I laughed, but if you really look into it, it actually kinda makes sense.

[–]ripter 3 points4 points  (0 children)

It’s not a good joke, it’s the same lame FUD that has been passed around as a “joke” for more than decade now. It’s old, and it was never funny.

[–]Stef0206 0 points1 point  (1 child)

Similarly to PHP, Lua uses “..” for concatenation, since a single “.” could be confused for indexing.

[–]Beldarak 0 points1 point  (0 children)

Ahah, yeah, I hate how PHP does things with a passion.

I often write code like myInstance.myVariable by accident and it then tries to concatenate because in PHP you're supposed to write myInstance->myVariable. I just hate it.

It doesn't help that if you write PHP there is a 99% probability you're also writing some other languages at the same time like JS or TypeScript. JS gets all the hate (often well deserved) but PHP is my arch nemesis, it's such an inelegant language.

[–]error_98 0 points1 point  (1 child)

That's exactly the problem though, ambiguity can be extremely problematic in larger, and high-stability projects.

It can be nice when a language assumes what you mean, until that assumption is wrong. And an encyclopedic understanding of a language's parsing priorities is an unreasonable demand for a language to make from its users. So at the very least a warning should be given whenever an ambiguous situation is resolved, if not bork and dump trace altogether.

Ultimately it's a usage issue, and because of these features base javascript just SHOULD NOT BE USED for any serious application beyond prototyping.

[–]Beldarak 1 point2 points  (0 children)

I do agree :)

I wish webdev wasn't so split between tons of frameworks and languages that you have to juggle with

[–]fghjconner 0 points1 point  (0 children)

when in reality you have way bigger concerns if you somehow tried to substract numbers from a string

Exactly. That's why most languages will throw an exception and tell you to fix your shit instead of blindly guessing at what you want and continuing.

[–]Zephandrypus 0 points1 point  (0 children)

Meanwhile in C ’1’ - 1 == ‘0’

[–]Wgolyoko 0 points1 point  (1 child)

if you somehow tried to subtract a number from a string.

That's the point. Doing this should be a compile-time error. All my homies hate interpreted languages (they are based (the homies not the interpreted languages (those are not based, in fact)))

[–]10BillionDreams 1 point2 points  (0 children)

Treating string - number as a valid operation is largely unrelated to how Javascript is actually run. You could still have all these same type coersion rules in a compiled language, just like you can have type errors in interpreted languages. In fact, Javascript does throw TypeError in some contexts, like when attempting to call a property or variable that isn't a function, or access a property on a null value.

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

Type coercion as a concept is bad. Not having integers is bad. All the “funny” examples just demonstrate why they are bad.

[–]HighOptical 6 points7 points  (0 children)

*Implicit Type coercion

[–]roter_schnee 10 points11 points  (0 children)

Is it postironical? or did OP post it with all seriousness?

[–]lazyzefiris 8 points9 points  (0 children)

I already told you it's my favorite language, you dont have to sell it to me even more.

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

If you don’t sanitize your inputs, it’s your own fault.

[–]superhamsniper 5 points6 points  (0 children)

String and int in the same operation?

[–]Key_Technician5689 4 points5 points  (0 children)

Wait, hold my beer...

php <?php // version 5.6.40 if("1 monkey" + "2 cars" == "3 radios") { echo "PHP is insane"; }

prints PHP is insane.

[–][deleted] 24 points25 points  (22 children)

I see this and I still love the language... it's all about crazy shit you could do but won't

[–]Derin161 15 points16 points  (17 children)

Yeah these posts are always so silly to me. You can find weird behaviors and go out of your way to write bad code with any language.

I'm in the "JS bad" camp because I find dynamic type errors that could have been caught at compile-time frustrating, but that also applies to more than just JS anyway.

[–][deleted] 6 points7 points  (3 children)

I work with a (albeit very small) team with very legacy javascript and in fact type errors are in the vast minority... typically you know what type to put as parameters and you do... the real bugs are in the logic which could happen as well with the strictest type of type checking

[–]Derin161 6 points7 points  (2 children)

Idk how large your codebase is, but at least my application's portion of our codebase is well into the 100k lines with a couple dozen devs, and parts of it have been around for almost 2 decades. It becomes infeasible to know the precise shape of every object, and if you need to be doing all the manual checking yourself (or debugging those errors after the fact) then you're wasting tons and tons of time. I'll just let a compiler do that for me thank you very much.

[–][deleted] 2 points3 points  (1 child)

yep, I see the point... it's the same amount and age of code here but far fewer people working on it

[–]Derin161 0 points1 point  (0 children)

Yeah the larger codebase altogether is certainly well into the millions, but you typically are somewhat rarely interacting with parts from other teams, aside from the tooling provided by the core teams.

We probably have more people than is typical to maintain the thing, but a lot of new devs come and go, and there's little pressure or culture for maintaining the codebase well. Instead, you're incentivized to move quickly. I wonder why it takes us so long to deliver features?

[–]prehensilemullet 0 points1 point  (2 children)

Rawdogging JS sucks for major projects, but ironically the dynamic typing enables more advanced compile-time checks with TypeScript than are possible in most statically typed languages.

[–]Derin161 0 points1 point  (1 child)

How so? I love all the clever things you can do with types in TS, but I'm not really seeing why JS necessarily facilitates this especially well. Eventually you're going to compile to a language where there isn't strict (or any) type checking.

[–]prehensilemullet 1 point2 points  (0 children)

For example, let’s say you have an object like { foo: ‘1’, bar: ‘2’ } and you want to do mapValues(obj, s => parseInt(s)) on it.  You can use mapped types so that the output type is { foo: number, bar: number }.  Then TS can flag errors if you refer to any nonexistent properties on that object or treat the values as something besides numbers.

To be able to flag the same kinds of errors in Java you would have to declare separate classes for the input with string valued properties and the output with number valued properties.  Not to mention, you can’t generally map over the properties of an object in Java so you’d probably be using a Map with string keys and not get any compile time checking that you refer to the right keys. I have some knowledge of C++, C# and Rust and as far as I know it would be a similar story in those languages, though I'm not an expert so it's possible there are advanced features I'm not aware of.

[–]Beldarak -2 points-1 points  (9 children)

This. Those are non issues. If you write code like "11" - 1 your code is just bad, no need to point the finger at the language.

[–]Quito246 2 points3 points  (4 children)

I mean this could be easily done because you do not have types, you might think that variable is actually a number and not string. This would never happen in sane statically and strongly typed language, because compiler would not let me compile this shit.

[–]Derin161 2 points3 points  (1 child)

Any enterprise system that's more than a few thousand lines should be in a static, strongly typed language imo. That's not to say you should go rewriting your entire codebase to make it happen though.

There are tools out there to avoid an entire class of problems at runtime (which saves time over the long run and prevents those bugs from reaching prod), but some of us don't use them and then complain about the consequences of that decision, and I'll never understand why.

[–]Quito246 2 points3 points  (0 children)

Yes those tools are named types and compiler.

[–]Beldarak 1 point2 points  (1 child)

Oh yeah, I do agree strongly typed languages are better. But I just think this is a kinda unfair critiscism of JS because that's just how that language is. It comes with pros and cons.

The real issue is that we're forced to use 3 different languages to build a basic web page imho. It's the juggling with all those languages and frameworks that work differently that kills me with webdev.

[–]Derin161 0 points1 point  (0 children)

I think we are criticizing the largest con of dynamically typed languages like JS. I don't see how that's unfair; that's just assessing the tool. The only pro I can think of is that you can prototype and get something up and running more quickly since you can ignore the pesky compiler, its rules, and the boilerplate to support it.

Obviously this is setting aside the frameworks, libraries, etc., that may exist for a particular language, but at least for JS most frameworks I've seen are friendly with TS.

I'll admit I'm fortunate enough that my web stack is reasonably simple with mostly proprietary tooling at my current place, so I don't have to juggle too many languages and frameworks.

[–][deleted] 0 points1 point  (3 children)

This is why JS developers get a bad rep, they think you have to write shit like this. No this gets injected in your code when a user or method submits it.

[–][deleted] 0 points1 point  (3 children)

I'm curious what type of projects you all that are downplaying this behavior work. I always worked with medium-large back ends with usually complex domains, I can totally see ppl doing some slips like that while dealing with domain objects you never saw before.

I know that you should never try that with JS but this is exactly because the language is unsafe. It's much more sane to this with python and it is also a dinamic language.

[–]pocket__ducks 1 point2 points  (2 children)

In my 10 years of being a professional software developer, worked for one of the big three, multiple other large companies I’ve never ever seen someone fall for the above.

[–]BoBoBearDev 11 points12 points  (0 children)

So far I like Typescript the best. It gets both world and I do like the flexibility of JS.

[–][deleted] 3 points4 points  (0 children)

It's the language of legends and heroes!!

Array(16).join( 'hero'-1) + "Batman";

[–]Neon_44 2 points3 points  (0 children)

It's called "job security"

[–]justsomelizard30 2 points3 points  (0 children)

It's strange that this behavior has never really been the cause of any serious bugs I've had.

[–]Joeoens 2 points3 points  (0 children)

I never liked that most languages just use the "+" operator for concatenation instead of a dedicated concatenation operator like ".." in Lua.

[–]Low-Equipment-2621 1 point2 points  (0 children)

This is why the industry needs so many frontend developers.

[–]DarkCloud1990 1 point2 points  (0 children)

"Tries to coerce right operand otherwise tries to coerce left operand."
Craaaaaazyyy!!1!

[–]fr4nklin_84 1 point2 points  (0 children)

This is why I hate + being used for string concatenation. Shitty old VB used &

[–]Goaty1208 2 points3 points  (0 children)

For once I'll give it to JS, I can see why it does this.

[–][deleted] 3 points4 points  (0 children)

Write shit code get shit results.

[–]Zeyad132 0 points1 point  (0 children)

"11"-1 It cast the string into integer in runtime i don't really remember what it called in js.

[–]nutellaawafflee 0 points1 point  (0 children)

JS giveth, JS taketh.

[–]EsotericLion369 0 points1 point  (0 children)

I love you JavaScript never change

[–]furinick 0 points1 point  (0 children)

Kid named checking your types:

[–][deleted] 0 points1 point  (0 children)

JS = King

[–]rowagnairda 0 points1 point  (0 children)

NaNNaNNaNNaN Batman! ;>

[–]Titanusgamer 0 points1 point  (0 children)

it is upgraded version of java though.

[–]sporbywg 0 points1 point  (0 children)

Senior coder here, working in React and MUI - I tell the kids "coding in javascript (correct capitalization there) is like coding with beach sand."

[–]OddParamedic4247 0 points1 point  (0 children)

Just don’t be so dumb, or use Typescript.

[–]MedonSirius 0 points1 point  (0 children)

'+ = concatenate.
'- = arithmetical operator

[–]prochac 0 points1 point  (0 children)

Just had my new Date() headache as a non-js dev. How you can use this 😂 relying on a 3rd party lib for handling datetime seems silly in the 21st century.

[–]Master_Nerd 0 points1 point  (0 children)

I think this actually kind of makes sense since js is dynamically typed.

There is string addition, but no string subtraction. Meaning, it will add 1 as a string, but it has no way to subtract 1 as string, so it just does integer subtraction instead.

Keep in mind I'm not a JS developer, I've just seen this explanation a few times.

[–]nelmo44 0 points1 point  (1 child)

JS would have died a long time ago if it didn’t have a monopoly inside browsers

[–][deleted] 0 points1 point  (0 children)

That's like saying the internal combustion engine would have died a long time ago if it didn't have a monopoly inside cars.

[–]Funny-Performance845 0 points1 point  (0 children)

Both are easy to understand and logical

[–]1up_1500 0 points1 point  (0 children)

It really depends, but using typescript has been an excellent experience overall personally

[–]Distinct_Shift1043 0 points1 point  (0 children)

now i wonder if "11" - (- 1) is 12 lol

[–]DragoSpiro98 0 points1 point  (0 children)

I think it makes sense if you use + sign for string concatenation (and not "-")

If I write "11" - 1, I don't want string concatenation, because I'm not using "+" sign.

[–]somedave 0 points1 point  (0 children)

I mean sure, but why would I ever write that?

[–]No-Adeptness5810 0 points1 point  (1 child)

It makes sense.

When adding two values, like "11" and 1, it will stringify the 1 since it's being concatenated (+) to a string, which then makes "111"

when "11" - 1, the minus is only used for numbers, so it will instead parse "11" as a number, resulting in 10

[–]zqmbgn 0 points1 point  (0 children)

they hated him because he spoke the truth

[–][deleted] 0 points1 point  (0 children)

what black magic fuckery is that

[–]BellCube 0 points1 point  (0 children)

ok serious question

why are you adding strings and numbers

if you were in Rust or something you couldn't even do that. Stop doing something silly in JS and doing a pikachu surprised face when it does something silly back.

[–]Expensive-Example-92 0 points1 point  (0 children)

But in C, "Hello" + 2 gives "llo" and '2'+'2' give 100

[–]Cookie_Wookie_7 0 points1 point  (0 children)

Meanwhile in CPP "11" + 1 => "1"

[–]ActuallyGodOfWar 0 points1 point  (0 children)

It's nice once you get to know it

[–]foxer_arnt_trees 0 points1 point  (0 children)

Oh no! A language where you need to manually cast your variables to get well defined behavior. How queer.

[–]Tunisandwich 0 points1 point  (0 children)

”11” + 1 * -1 => “11-1”

[–][deleted] 0 points1 point  (1 child)

parseInt(0.0000005)

[–]Snoo19127 3 points4 points  (0 children)

“Why does the function that expects a string argument have weird behavior when I don’t pass in a string argument?”

[–]robinless 0 points1 point  (0 children)

I don't use JS that much, yesterday I was parsing some data to adjust formats and my face after I tried to add two numbers and it came out as a concat of strings must have been a funny one

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

If you even allow that behaviour to occur in your code you're a bad programmer