13
11
10
u/killermenpl Oct 03 '25
I never got this argument against JS. "When you use this explicitly documented feature of the language, it does exactly what the spec said it would, not what you'd expect".
When you try to do operations on mismatched types, the runtime will make a best effort to convert the operands into the same type - string in the first case, number in second case. It's not a bug, it's not a "quirk". It's an intentional design decision
7
u/Scared_Accident9138 Oct 03 '25
In a well designed language you don't have to look into the documentation for inconsistencies
6
u/killermenpl Oct 03 '25
But it's not an inconsistency. The language is very consistent with that. Saying that you need to look at docs to know about type coercion is like saying you have to look into Rust docs to know about the borrow checker.
There are valid criticism about the language. Things like
Date, or just how much the backwards compatibility is holding the language back. But complaining about type coercion is pure skill issue0
Oct 04 '25
Amen to that. A hammer is heavy to drive in nails, if you let go it above your foot and it hurts your toes, learn to hold things better or use a safety hammer(TS).
To be honest the hate for JS makes me love it more. I'm not trading my freedom for (type) security. I'd rather just get better at not making mistakes 😎
6
5
u/PwnTheSystem Oct 03 '25
The plus (+) operator has two uses:
Adding two numbers
Concatenating two strings
If the first value is a string and it's a + operator following it, then treat the operation as a concatenation.
The minus (-) operator only has one use:
Subtract from the first value an amount equivalent to the second value
It's obvious that, from this token parsing logic, that the minus operator cannot subtract a string. So it casts that value to a number.
It's simple. JavaScript does make sense in that aspect
6
2
u/djmisterjon Oct 03 '25
Why would you want to add a string and a number?
It's an antipattern in programming.
6
u/Scared_Accident9138 Oct 03 '25
It's more about what happens if the types end up being like that. One thing that's always annoyed me is if you for example forget to parse a string as a number and then call a function and get this behaviour
3
u/djmisterjon Oct 03 '25
This is one of the main reasons why using TypeScript is essential. It adds an additional layer of static type checking to js and helps prevent type polymorphism, which can be detrimental to V8 engine optimizations.
If you want your code to be optimized closer to the lower-level constructs of the C language, and have a JavaScript application that runs with high performance, polymorphism must be avoided. It acts as a trigger that prevents certain internal engine optimizations.
1
u/GeneralBendyBean Oct 03 '25
I've honestly never encountered a bug or blocker who's root cause was type confusion. Maybe I have, but it's so easy to solve.
5
u/marslander-boggart Oct 03 '25
Why would you want to add a variable to a variable? It's an antipattern.
0
Oct 03 '25
[deleted]
1
u/denisbotev Oct 03 '25
If you want to add two numbers, x and y, and you didn't cast both to int, e.g. x=12 and y="12", instead ot 24 you end up with "1212"
1
1
u/asgwins Oct 03 '25
This CS major slop doesn't make sense. People who write real code and ship real software with users don't care about these weird quirky edge cases. Just don't do this? It will never be relevant, you will never write this line of code in your entire life ever.
1
u/navetzz Oct 03 '25
I hate what they made out of javascript (a scripting language shouldn't have evolved to what it is know)
However, if you are adding (or substracting) strings with integers, you deserve what you get.
1
1
u/itsjakerobb Oct 04 '25
This video is a few minutes long, hilarious, and very informative for anyone who uses JavaScript.
2
1
1
u/DefenitlyNotADolphin Oct 04 '25
the real question is why are you adding a string to a number without making the types equal? Like yeah do stupid things except stupid things
1
1
1
1
1
1
1
u/VeryFriendlyOne Oct 06 '25
Why is it like that? They make sense individually, but both at the same time...
1
1
u/bookaddicta Oct 07 '25
As someone who’s never used JavaScript, what the heck is that abomination of logic
1
u/maria_la_guerta Oct 07 '25
The point of JS originally was that it's failure should never ever block the HTML for rendering. It was built in 10 days for the sole purpose of showing popups, and it's on us for using it for everything under the sun 30 years later.
That being said TypeScript is amazing and nobody should be writing vanilla JS anymore anyways.
1
u/warharobrine Oct 07 '25
Why does java script use binary positions for one and numerals for the other
1
u/bubbybumble Oct 08 '25
Lol people always point this out as an issue but you shouldn't do stuff like this. I guess the real problem is it doesn't error out to let you know you should be more specific
1
u/Junaid_dev_Tech Oct 23 '25
Yep... Yes, FUCK YOU JAVASCRIPT
Edit : Don't get me wrong, but it's my frustration on JS. 1 st "yep" means "ok, I can adjust with it" then 2nd "yes" means "What the Heck, No I am out"
0
0
174
Oct 03 '25
[removed] — view removed comment
114
u/vvf Oct 03 '25
Probably because
-has no string override so it coerces to number, while+does have a string versionThis is why we have linters/TS
62
u/BangThyHead Oct 03 '25
That could be a neat override/operator!
x := "hello world, I'm home" y := ", I'm home" fmt.Println(x - y) // "hello world"49
u/vvf Oct 03 '25
lol. The type of operator you’d use once every 3 years
7
u/BangThyHead Oct 03 '25
Or whenever I have to switch to a language I can't remember the specifics of and I don't have an IDE.
You have to true 5 different string manipulation methods to figure out which one actually complies.
Thinking, which one is the "drop a suffix?":
Strings.ReplaceLast(x, y, "") x.ReplaceN(y, "", -1) Strings.TrimSuffix(....) // And if the '-' operator removed the greatest suffix match, // And didn't require a full suffix match. // And we didn't care about efficiency: for index, char in x: remainingLettersInX = x.size - index if remainingLettersInX > y.size: continue if x[index:] == y[:remainingLettersInX]: x = x[:remainingLettersInX] breakWhenever I come back to Java after a long time, I always forget the string functions.
8
7
u/cowlinator Oct 03 '25
x := "hello world, I'm home" y := "supercalifragileistic" fmt.Println(x - y) // exceptionUghhh
1
1
u/JackReedTheSyndie Oct 03 '25
But what happens when y is not a substring of x?
3
u/Intelligent-Wind-379 Oct 03 '25
Same thing that happens when you try to multiply two strings
5
2
u/BangThyHead Oct 03 '25
While this would be a poor/unclear operator, I assume it would do the same thing as 'remove suffix' when the suffix doesn't match. It does nothing.
1
u/Connect_Detail98 Oct 05 '25
I mean, just fail? Just say "I can't substract an integer from a string. K bye."
Much better than having this weird ass thing happening. In fact, never allow operations between different data types unless it is logical. "11" + 1 is not logical at all.
1
u/vvf Oct 05 '25
Unfortunately we are stuck with weird JavaScript default behavior to prevent breaking older websites, in case they relied on this stuff. Hence the use of layers on top to handle issues instead, such as TypeScript, which will not “compile” when it sees stuff like this.
1
u/Connect_Detail98 Oct 05 '25
Yeah, keeping backwards compatibility sucks... I'd create a breaking change and then a tool that helps people detect every single place in their code that has to be fixed with the equivalent code using the new syntax.
That way it's not just a "good luck everyone..." situation but a more guided migration towards a better future for us and our future generations.
1
u/vvf Oct 05 '25
And for the websites that don’t have an active developer? Or government/healthcare websites with glacial development cycles?
A better future for us and our future generations
I understand you probably have an innate desire for order and correctness, and that this unusual behavior annoys you. However, we have much bigger problems to tackle before cleaning up weird JavaScript behavior.
Most devs who are taking these problems seriously already have TypeScript and linters to catch errors, at which point type coercion and other quirks become irrelevant. It’s ultimately a very low cost to developers.
1
u/Connect_Detail98 Oct 06 '25 edited Oct 06 '25
Why would websites that don't have active devs be affected by a new version of JS dropping? No devs means no upgrades in JS versions, which is kinda concerning.
Keep the old version, do security patches on it for a couple of years. Add the tool that helps people migrate easily. Remove all the weirdness in the new version. Drop support of the old version.
I feel a big chunk of the JS community hates TS. In my company TS is taboo and people go straight for JS... But when I'm doing "a === b" I'm always like "this shit is certainly going to blow up on my face and I have no idea why yet". Every single comparison between two things makes me feel like something bad is going to happen. Like "wait what if 0 === undefined is actually true?".
I'm not a JS developer, but I need to write JS sometimes, so I get that it's a skill issue... But why don't I have this skill issue in other languages that I don't dominate?
Just make it a better language. It can be. But I get priorities, what's the big priority in JS right now?
1
u/vvf Oct 06 '25
Because the browser runtime will change? Do you know how JavaScript even works in web pages?
They ARE building the “new version” and have been doing so for years — and I’ve already explained how devs can easily avoid the old, shitty version.
But why don't I have this skill issue in other languages that I don't dominate?
Because unlike every other language, you have zero control over the interpreter of the language. You’re writing code that will run in someone else’s browser. The language spec reflects that. The best practices around JS development reflect that.
Besides, the worst of the worst has already been excised from the language. Modern browsers refuse to support these old features. But it moves slowly, and again it’s just not that big of a problem when you’re actually working on it. So we’re left with quirks like
===and maybe you should just get over it lol.1
u/Connect_Detail98 Oct 06 '25
So the browser can't work with 2 different ES versions? I'm pretty sure it can. That's exactly why those pages that aren't maintained still work, because the browser supports recent and old ES versions.
1
1
u/Questioning-Zyxxel Oct 07 '25
And exactly by what magic would the browser select different language implementations?
You think the javascript code starts with a line <type text/javascript-4.2> or similar to try and match initial version lines for HTML? Javascript can be embedded just about everywhere.
→ More replies (0)4
u/DonutConfident7733 Oct 03 '25
"11"+1==="111"
wtf is that operator?
12
u/vvf Oct 03 '25
Equals but like, even more equal
(It doesn’t perform type coercion so you don’t get wacky stuff like
'1' == 1)3
u/maxymob Oct 03 '25
The standard stricter one people actually use because double equal only invites chaos
1
u/DonutConfident7733 Oct 03 '25
I wasnt asking what it is, more wondering why the fuck it exists... Other languages don't need such an operator.
2
u/caerphoto Oct 03 '25
Other languages don’t try to avoid exceptions by “helpfully” converting types automatically.
Other languages also had more than 10 days’ development time.
1
3
1
u/DensityInfinite Oct 04 '25
Was hearing a lecture on JS where almost everyone in the room came from a previous course that had us write C.
Lecturer proceeded to show off some of stuff JS can pull off. The entire room was in shambles. Still funny to this day.
47
u/GhostingProtocol Oct 03 '25
I can never go back to dynamically typed languages. I don’t understand how people find them easier…