- cross-posted to:
- [email protected]
- cross-posted to:
- [email protected]
javascript is to web developers what powerpoint is to sales people
What no type safety does to an MF…
It makes sense though
It does to some degree.
- “11” is string, 1 is an int, because strings can be added (+) convert int to string and combine: “11”+“1” = “111”
- “11” is string, 1 is an int, because strings cant be subtracted (-) convert string to int and combine: 11-1 = 10
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.
This here is my absolute favorits way to diss someone. Send the a wikipeda link and bam!
… It does?
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
Imagine doing math with strings and then blaming the language not yourself
The problem is consistency.
The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly ‘helpful’ in assuming intent.
Sure, but at this point it’s your own fault if you don’t use Typescript to keep these issues from happening.
“Use a different language” is a common defense of javascript, but kind of a weird one.
Not really, considering Typescript only adds static types to JS. It’s not a different language, it’s an extension.
Since it needs to be compiled to JavaScript in order to be used, I kind of consider it a different language. Yes, it’s a strict superset of JavaScript, but that makes it different.
That’s your prerogative, but it honestly doesn’t make sense. Typescript adds almost no functionality to JS (and the few pieces it adds are now considered mistakes that shouldn’t be used anymore). It only focuses on adding typing information, and in the future you’ll be able to run TS that doesn’t use those few added features as JS (see the proposal).
You can also add the TS types as comments in your JS code, which IMO shows that it’s not a different language.
So, just don’t use JavaScript?
That’s also my understanding: “Javascript is great because you can use other languages and then transpile them to JS.”
Oh man machine language is so good, literally the best actually
JS itself is great, I prefer it to most other languages due to the flexibility that it allows. Adding types through TS to safeguard against footguns doesn’t mean you’re not still using JS. You can also add the types using comments instead if you prefer it, which means you’re actually writing raw JS.
I wouldn’t use raw JS for anything new, yes. Typescript however is an excellent language.
Yeah! Wasm is a thing. At least rust and go are pretty neat in the browser lately.
We should leave that pile of semantics and just go further with web development
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.
Point taken but the one I use is only ~200k for the whole package, ~11k for the actual file that gets loaded
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
If you’re consciously and intentionally using JavaScript like that, I don’t want to be friends with you.
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 🤭
Yeah, this looks dumb on the surface, but you’ve got bigger problems if you’re trying to do math with strings
Better than doing physics with strings
Yup. It’s completely inconsistent in its interpretation of the + operator.
Yeah, I actually had to try 1+“11” to check that it didn’t give me 12, but thankfully
it commutesit’s consistent 😇it commutes
Maybe the behaviour with regard to type conversion, but not for the operation itself.
“13”+12 and 12+“13” don’t yield the same result.
Nor would I expect “1312” to equal “1213”… Still that operator with these operands should just throw an exception
Given it’s JavaScript, which was expressly designed to carry on regardless, I could see an argument for it returning NaN, (or silently doing what Perl does, like I mention in a different comment) but then there’d have to be an entirely different way of concatenating strings.
expressly designed to carry on regardless
I’m surprised they didn’t borrow
On Error Resume Next
from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing wasOn Error Resume
.On Error Resume Next
at least moved on to the next line of code when an error occurred;On Error Resume
just executed the error-generating line again … and again … and again … and again …Why would you need an entirely different way of concatenating strings? “11” + 1 -> exception. “11” + to_string(1) = “111”
It’s just doing a lot of stuff for you that it shouldn’t be in first place 🤭
Kinda like log4j!
From all the Javascript quiks this is the least stupid and the most obvious.
pro tip:
"ba" + 0/0 + "a"
This has got to be baNaNa
That is absolutely
(n > 1) * ("ba" + 0/0 + "a")
(n > 1) * (“ba” + 0/0 + “a”)
Uncaught ReferenceError: n is not defined
?
🫣
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.
I mean, I’d never try to do this anyway because if the types aren’t the same unexpected things can happen. That’s like programming 101.
Exactly, which is why designing the language to allow it is incompetence.
Fair enough.
It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn’t, it reverses operand positions and tries again.
The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.
Computing a nonsensical result is itself a failure. Continuing to run while avoiding giving an error in that case accomplishes nothing but to make the program harder to debug.
Thanks for saving me the typing.
It’s an issue with most if not all languages that aren’t strongly typed.
Perl is an old but notable exception. + is purely for addition in the base language.
If you try to add two strings with it, they’ll be converted to numbers based on any number-like characters they have at their left hand ends, and, if warnings are enabled (and you should definitely do that), you’ll get runtime warnings about it if there’s even anything vaguely non-numeric about them.
e.g. “1”+“11” will get you 12 with no complaint, warnings or otherwise. Not even the string “12” either, although it’s hard to determine one from the other in Perl. It’s a need-to-know kind of thing. And you generally don’t.
“a”+“bb” gives 0 as the result because they’re not numbers and “1a”+“11bb” will give 12, but these latter two will give warnings. Two each, in fact, one for each dodgy parameter.
String concatenation is done with the dot operator instead. “1”.“11” gives “111”. This comes with it’s own minor problems, but at least + is safe.
That’s because Perl doesn’t do operator overloading in general. Even the equality operator is different for strings (
eq
instead of==
). As a language, it may look pretty weird and lack some modern features, but the underlying design is surprisingly intelligent and consistent in many ways.Not strictly true.
Perl’s default bitwise operators do differentiate between numbers and strings that look like numbers in a way that addition doesn’t*, and the readline/glob operator
<>
does different things depending on what (if anything) is between the signs.There’s also the whole
overload
pragma for objects, which doesn’t affect default data types, but if you’re sufficiently perverse, you can define a String class that uses ‘+’ like JavaScript.* in 2015, they added new operators so that those and the original operators don’t overload and have only one specific purpose if the
bitwise
pragmaEdit: feature is turned on. You might know all this already though.
I think I’m on the side of “if you do this in your code, you deserve what you get.”
Fuck me.
That is just the tip of the iceberg:
so plus coerces into string if not number, was that so hard?
Haha that’s a great site. But I think the C example is actually reasonable behaviour.
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
Ugh, like… I get why it outputs like that, but I also absolutely hate that it outputs like that.
It’s my favorite language too, but I also find this hilarious.
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.
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.
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.
This is exactly why it should throw an error, to make it incredibly obvious something isn’t working correctly so it can be fixed. Otherwise you have wrong logic leading to hard to notice and hard to debug problems in your code
Use typescript
No. I don’t want to transpile. I don’t want a bundle. I want a simple site that works in the browser. I want to serve it as a static site. I don’t want a build step. I don’t want node_modules. I want to code using the language targeted for the platform without any other nonsense.
Javascript is cancer. Fucking left pad?! How the fuck did we let that happen? What is this insane fucking compulsion to have libraries for two lines of code? To need configuration after configuration just to run fucking hello world with types and linting?
No, fuck Typescript. Microsoft owns enough. They own where you store your code. They own your IDE. They might own your operating system. Too much in one place. They don’t need to own the language I use, too.
“Let’s use a proprietary improvement to fix the standard that should have not sucked in the first place” is why we can’t have nice things.
No.
I’d rather have my website shit itself than have silent difficult to find errors.
Use typescript
In practice runtime errors are a bitch to find and fix.
Fair enough. This is why people prefer typescript
Obligatory link to wat? video
Type of “not a number” is number
[object Object][object Object]
The fun strings to enter in web forms once in a while.
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.
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.
You are entitled to your opinion. implicit conversion to string is not a feature in most languages for good reasons.
Sure. And you’re entitled to yours. But words have meaning and this isn’t MY OPINION, it’s objective reality. It follows strict rules for predictable output, it is not nonsensical.
You’re entitled to think it’s nonsense, and you’d be wrong. You don’t have to like implicit type coercion, but it’s popular and in many languages for good reason…
Language Implicit Coercion Example JavaScript '5' - 1 → 4
PHP '5' + 1 → 6
Perl '5' + 1 → 6
Bash $(( '5' + 1 )) → 6
Lua "5" + 1 → 6
R "5" + 1 → 6
MATLAB '5' + 1 → 54
(ASCII math)SQL (MySQL) '5' + 1 → 6
Visual Basic '5' + 1 → 6
TypeScript '5' - 1 → 4
Tcl "5" + 1 → 6
Awk '5' + 1 → 6
PowerShell '5' + 1 → 6
ColdFusion '5' + 1 → 6
VBScript '5' + 1 → 6
ActionScript '5' - 1 → 4
Objective-J '5' - 1 → 4
Excel Formula "5" + 1 → 6
PostScript (5) 1 add → 6
I think JavaScript is filthy, I’m at home with C#, but I understand and don’t fear ITC.
Also, you contradicted yourself just then and there. Not a single of your examples does string concatenation for these types. It’s only JS
- In https://lemm.ee/comment/20947041 they claimed “implicit type coercion” and showed many examples; they did NOT claim “string concatenation”.
- However, that was in reply to https://lemmy.world/comment/17473361 which was talking about “implicit conversion to string” which is a specific type of “implicit type coercion”; NONE of the examples given involved a conversion to string.
- But also, that was in reply to https://lemm.ee/comment/20939144 which only mentions “implicit type coercion” in general.
So, I think probably everyone in the thread is “correct”, but you are actually talking past one another.
I think the JS behavior is a bad design choice, but it is well documented and consistent across implementations.
Read the thread again, it seems you slipped somewhere. This was all about the claim that implicit conversion to string somehow could make sense.
C# is filthy. But it explains where you got your warped idea of righteousness.
Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.
- should also do some string action
Like what kind of string action?
“Hello” + " world" is what everyone can understand. Switch with “-” and it becomes pointless.
this the “or throw an error”
If you try what I wrote it will throw a NaN. I was asking about the first part of the proposal.
The NaN isn’t an thrown. It’s just silently put into the result. And in this case it’s completely unintelligible. Why would an operation between two strings result in a number?
"Hello" - "world"
is an obvious programmer mistake. The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.The main problem here is downward coercion. Coercion should only go towards the more permissive type, never towards the more restrictive type.
Coercing a number to a string makes sense, because each number has a representation as a string, so
"hello" + 1
makes intuitive sense.Coercing a string to a number makes no sense, because not every string has a representation as a number (in fact, most strings don’t).
"hello" - 1
makes no sense at all. So converting a string to a number should be done by an explicit cast or a conversion function. Using-
with a string should always result in a thrown error/exception.The interpreter knows that this is not something anyone will ever do on purpose, so it should not silently handle it.
You basically defied the whole NaN thing. I may even agree that it should always throw an error instead, but… Found a good explanation by someone:
NaN is the number which results from math operations which make no sense
And the above example fits that.
"hello" - 1
makes no sense at all.Yeah but actually there can be many interpretations of what someone would mean by that. Increase the bytecode of the last symbol, or search for “1” and wipe it from string. The important thing is that it’s not obvious what a person who wrote that wants really, without additional input.
Anyway, your original suggestion was about discrepancy between + and - functionality. I only pointed out that it’s natural when dealing with various data types.
Maybe it is one of the reasons why some languages use . instead of + for strings.
That’s the case in many languages, pretty much in all that don’t have a separate string concatenation operator.
Yeah, and almost all languages I know then would throw an exception when you try to use
-
with a string, and if they offer multiple operators that take a string and a number, they always only perform string operations with that and never cast to a number type to do math operations with it.(e.g. some languages have
+
for string concatenation and*
to add the same string X time together, so e.g."ab" * 2 => "abab"
. It’s a terrible idea to have+
perform a string operation and-
performs a math operation.)Sure, but then your issue is with type coercion, not operator overloading.
Because there’s in fact no operator overloading happening, true, but that’s mostly an under-the-hood topic.
It should not happen no matter why it does happen under the hood.
Operator overloading for
string - string
is wrong and type coercion to implicitly cast this toint(string) - int(string)
is just as wrong.There is operator overloading happening - the
+
operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.It should not happen no matter why it does happen under the hood.
If you don’t want it to happen either use a different language, or ensure you don’t run into this case (e.g. by using Typescript). It’s an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.
Lol. In a dynamically typed language? I will do this always, that’s why I am using it
You can have a dynamic language that is strongly typed to disallow stuff like this. Like Python for example
Aand what is your point?