JavaScript
JavaScript
JavaScript
This is too stupid so I had to check.
Fuck me.
Hm, playing devil's advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.
The first is assumed to be a concat because one of the parcels is a string...
It's just doing a lot of stuff for you that it shouldn't be in first place 🤭
From all the Javascript quiks this is the least stupid and the most obvious.
Unfortunately, it makes sense if you know what + means, which is concatenate. - is strictly a math function though.
Not saying that makes this better. It just makes sense.
It is 'comprehensible' in the sense that it's possible to figure out how it happened, but it absolutely does not "make sense" in terms of being a reasonable language design decision. It's 100% incompetence on the part of the person who created Javascript.
It's an issue with most if not all languages that aren't strongly typed.
I think I'm on the side of "if you do this in your code, you deserve what you get."
Fuck me.
Imagine doing math with strings and then blaming the language not yourself
Javascript is a dogshit language that everyone is stuck with. The best that we can hope for is the likes of typescript take the edge off of it. Even though it's like smearing marzipan over a turd. At least it's ok if you don't take a deep bite.
JS should have never leaved the Browser side. Now you can use this thing for Backend and is just awful
[object Object][object Object]
The fun strings to enter in web forms once in a while.
To start off... Using arithmetic operators on strings in combination with integers is a pure skill issue. Let's disregard this.
If you were to use + where one part is a string, it's natural to assume a string appending is desired since + is commonly used as a function for this. On the other hand, - is never used for any string operation. Therefore, it's safe to assume that it relates to actual artihmetics and any strings should therefore be converted to numerical values.
This is an issue with untyped languages. If you don't like it, use typescript. End of story.
Instead of trying to make it work, javascript could just say "error." Being untyped doesn't mean you can't have error messages.
This is fair enough from an idealistic view. In practice, you don't want your entire website to shit itself because of a potentially insignificant error.
I think it's less about type system, and more about lack of a separate compilation step.
With a compilation step, you can have error messages that developers see, but users don't. (Hopefully, these errors enable the developers to reduce the errors that users see, and just generally improve the UX, but that's NOT guaranteed.)
Without a compilation step, you have to assign some semantics to whatever random source string your interpreter gets. And, while you can certainly make that an error, that would rarely be helpful for the user. JS instead made the choice to, as much as possible, avoid error semantics in favor of silent coercions, conversions, and conflations in order to make every attempt to not "error-out" on the user.
It would be a very painful decade indeed to now change the semantics for some JS source text.
Purescript is a great option. Typescript is okay. You could also introduce a JS-to-JS "compilation" step that DID reject (or at least warn the developer) for source text that "should" be given an error semantic, but I don't know an "off-the-shelf" approach for that -- other than JSLint.
That is just the tip of the iceberg:
so plus coerces into string if not number, was that so hard?
Oh wow, that's upsetting
Not just javascript: https://www.destroyallsoftware.com/talks/wat
F#? What? We can't curse on the internet? Self censorship at dictator levels here. /s
Haha that’s a great site. But I think the C example is actually reasonable behaviour.
Ugh, like... I get why it outputs like that, but I also absolutely hate that it outputs like that.
People that try to do mathematical operations with strings blaming the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense because they realized it would be used by people that have no idea what they are doing. Clearly they were right.
the programming language that had a stated design goal to do its best and try to keep running scripts that make no sense...
...itself makes no sense. It is wrong and bad that Javascript was ever designed that way in the first place.
It was never intended to run full applications but only the small business scripts and hobbyist homepage stuff that were the thing in the 90s, across inconsistent browsers that were a jungle of hit and miss behaviour where it was preferred that menus keep working even if the mouse effect was not. Anything of scale was expected to be done in Java. Dynamic web pages did not exist and as anything not static was generated server side into a static html file to be rendered on the client.
Anyway, back then it wasn't considered the job of the programming language to hold the hand of the aspiring developer as it is common today. It's not a bad thing that IDE and even compilers and preprocessors try to help you write better code today, but then it simply didn't exist.
JavaScript is from a different time and because it has the hard requirement or backwards compatibility there is no changing it and has not been for thirty years except to add stuff to it.
I think it's just silly to ask the past to keep up with the present. Bad code is not the fault of the language regardless, even though junior devs and even seasoned ones like to think so to protect their ego. I think it is better to accept it, learn from it and roll with it because every single platform and language has their weird quirks anyway.
Signed, old dude that learned programming in 8 bit BASIC and 6502 machine code without an assembler, where code bad enough would freeze your machine that required a cold boot and starting over from your last save that you didn't do.
it would be used by people that have no idea what they are doing. Clearly
And so let's enable these people?
Let's add AI to the mix while we're at it.
There's also Chefkoch and Mozart for that purpose.
If you mix types like that, it's your own fault
BS. A language shouldn't have operators that allow non sensical operations like string concatenation when one operand is not a string.
Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.
It's not nonsensical, implicit type coercion is a feature of JavaScript, it's perfectly logical and predictable.
JavaScript is a filthy beast, it's not the right tool for every job, but it's not nonsensical.
When you follow a string with a +
, it concatenates it with the next value (converted to string if needed). This makes sense, and it's a very standard convention in most languages.
Applying arithmetic to a string would be nonsensical, which they don't do.
Obligatory link to wat? video
Lets fix it. I think that since we are removing the ones, then "11" - 1 should be equal to "".
Hear me out:
"11" - 1 = "11" - (-1) = "11" (did not find "-1" in "11)
Or
"11" - 1 = "11" - (-1) = "1" (removed first "1")
javascript is to web developers what powerpoint is to sales people
It makes sense though
.... It does?
It does to some degree.
I'm not into JS so I don't know how it takes priority. ints can be added too, so I guess its basing it on the first variable which is compatible with the operator: in the first case string, in the second case int.
If this is how it works, it makes sense. But imo its a case of the designers being preoccupied with whether or not they could, they didn't stop to think if they should.
Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type 'string' to 'int'!
This is a really good interview, and does a good job highlighting Javascript's biggest strength: it's flexibility.
“It was also an incredible rush job, so there were mistakes in it. Something that I think is important about it is that I knew there would be mistakes, and there would be gaps, so I made it very malleable as a language.”
He cites the “discovery” of asm.js inside of JavaScript, calling it “another thing I’m particularly proud of in the last 10 years.” It uses the bitwise operators that were included in the original JavaScript which are now the basis for a statically-typed language with machine types for high-speed performance. “If it hadn’t been in there from 1995, it would’ve been hard to add later. And the fact that it was there all along meant we could do incredibly fast JavaScript.”
He tells InfoWorld it’s “this very potent seed that was in the original JavaScript from the 10 days of May in 1995.” JavaScript’s 32-bit math operators (known as bitwise operators) trace their lineage all the way back to the C programming language — and to Java. This eventually led to WebAssembly — a way to convert instructions into a quickly-executable binary format for virtual machines — and the realization that with a JavaScript engine, “you can have two languages — the old language I did with the curly braces and the functions and the shift operators, and this new language which is a binary language, not meant for reading by humans or writing. But it can be generated by compilers and tools, and can be read by tools…”
This is my favorite language: GHC Haskell
GHC Haskell:
GHCi> length (2, "foo") 1
Wait, now I need to know why.
some time later *
I went to check why the hell this happened. It looks like the pair ("(,)
") is defined as an instance of Foldable
, for some reason, which is the class used by functions like foldl()
and foldr()
. Meanwhile, triples and other tuples of higher order (such as triples, quadruples, ...) are not instances of Foldable
.
The weirdest part is that, if you try to use a pair as a Foldable
, you only get the second value, for some reason... Here is an example.
ghci> foldl (\acc x -> x:acc) [] (1,2) [2]
This makes it so that the returned length is 1.
Oddly enough, in Haskell (as defined by the report), length is monomorphic, so it just doesn't work on tuples (type error).
Due to the way kinds (types of types) work in Haskell, Foldable instances can only operate over (i.e. length only counts) elements of the last/final type argument. So, for (,) it only counts the second part, which is always there exactly once. If you provided a Foldable for (,,,) it would also have length of 1.
I don't even know Haskell but it seems like (" ( , ) ") would be an instance of boob.
Feels like it could be one of those facebook posts to test "smart" people. Only the top 1% of people can answer this simple math question: "11" + 2 * 2 - 3
If you're consciously and intentionally using JavaScript like that, I don't want to be friends with you.
It's because +
is two different operators and overloads based on the type to the left, while -
is only a numeric operator and coerces left and right operands to numeric. But frankly if you're still using +
for math or string concatenation in 2025, you're doing it wrong.
I know nothing about javascript, what is wrong with using + for math? perhaps naively, I'd say it looks suited for the job
The correct way to do it is to load a 500mb library that has an add function in it.
It's much better to make your own function that uses bitwise operations to do addition.
function add(a, b) { while (b !== 0) { // Calculate carry let carry = a & b; // Sum without carry a = a ^ b; // Shift carry to the left b = carry << 1; } return a; }
(For certain definitions of better.)
The native arithmetic operators are prone to floating point rounding errors
Oh we've hit an issue that's solved by another language or we could make another framework
It's my favorite language too, but I also find this hilarious.
What no type safety does to an MF...
It's not that hard to understand what it is doing and why the decision was made to make it do that. JavaScript has a particular purpose and it's mission is not consistency.
It's not like TypeScript doesn't exist if you just get lightheaded at the idea of learning JavaScript's quirks and mastering using it despite them.
Scanned the article: neither mission, nor purpose, nor type coercion unga-bunga explained. Or was I expected to see the greatness of the language and be humbled by its glory and might?