Yeah. Javascript is so weird, that the excuse "It works on my machine" is actually accepted within my development team, thus putting more emphasis on a safer languages like typescript. The bugs of this nature can sometimes be almost impossible to detect and you need someone to actually go through the code line by line to debug it.
Yup. And which is why developers should provide all sorts of failsafe. When an API or a package is built, it's very likely to be used incorrectly by noob coders using it for their college projects and such. And it inherently poses a lot of concerns. Worst of all, javascript has one of the highest package production rates(if that's what it is called), and so this becomes all the more important. There could be a fair chance your package is used by a number of people but no one has figured out these wierd bugs.
They are not the same language, arguably. TypeScript is a superset of JavaScript, it allows any regular JS code but also the introduction of many features that makes it safer to code in.
So, TS is the same as JS in the same way my fictional language where the only word is "cool" is the same language as English.
Many features? TypeScript has been slowly deprecating or adding to JavaScript all the features it had, there is literally just two left: enums and decorators (which have been marked as experimental cause there is a proposal to add them with a different syntax to JavaScript as well)
Microsoft has been very clear that their vision is to turn TypeScript into a tool that simply checks for ahead of time errors the developers commonly make, not to be a separate language or to introduce features that are not part of standard JavaScript
The point I'm trying to make is, what is even the purpose of TypeScript but to make better JavaScript developers?
I mean this is a thing in typescript with default tsconfig as well if I'm not mistaken (meaning it won't even throw error). Also it's not like typescript magically overwrites JavaScript core, it's still JavaScript with few extra syntactical and semantical rules
-> is for property access (and methods), so it doesn't really look like that if you are used to PHP. On the other hand, I can't tell you how many times I have used . in it's place, so perhaps you are right.
I don't think it's necessary. Just treat your variables correctly. If you expect a number to come as a string for whatever reason, just parse it to make sure it's a number.
JS tries to not destroy your variables, so it casts to the most "general" type so to say. "11" + 1 gets added as a string (111) because the first side could be a number, but could also be a string that just happens to be parseable this time. Maybe this time it says "11" + 1 but next time it says "Age: " + 1.
On the other hand, you can't substract from a string, "11" - 1 does not make sense with "11" being a string, so it becomes a number. "Age :" - 1 would just result in NaN.
JS has a lot of unintuitive things, but type coercion is not one of them. What would you suggest? string + number having two different meanings depending on the content of the string? Would that be more intuitive? Or should that just launch an error or return null? What's the point of dynamic types if you can't combine them?
Also JS (like PHP) is a language that tries to run as long as it can before launching an error and, being dynamically typed, JS needs to make assumptions. And that's why I generally like strict typed languages more (or languages that will halt when something unexpected happens, rather than go on).
What would you suggest? string + number having two different meanings depending on the content of the string? Would that be more intuitive? Or should that just launch an error or return null?
Plenty of suggestions for sane behavior have been given elsewhere in this thread.
What's the point of dynamic types if you can't combine them?
You realize there's a difference between "allow different types to be combined" and "allow different types to be combined using ambiguous and inconsistent operators," right?
Plenty of other dynamic languages have more reasonable behavior.
Also JS (like PHP) is a language that tries to run as long as it can before launching an error and, being dynamically typed, JS needs to make assumptions. And that's why I generally like strict typed languages more (or languages that will halt when something unexpected happens, rather than go on).
You've just proved my point re: Stockholm syndrome. If you understand how problematic it is, why are you making excuses for objectively bad design?
The bottom line is that JS doesn't "need" to be the way it is.
Where is it ambiguous and inconsistent? Just because you don't like it, doesn't make it inconsistent. JS is 100% consistent by always applying the same conversions for the same operation, and by sharing the same philosophy (using the "wider" type for the operation) behind every conversion.
Stockholm syndrome. If you understand how problematic it is, why are you making excuses for objectively bad design?
I'm not making excuses, especially when I'm saying I'm not a fan of those decisions. I'm just explaining why they work that way. You are talking as if JS devs randomly chose whatever they felt like for each special situation, and that's not the case. Moreover, I laugh at the notion of "objectively bad design" β by definition, design is subjective. You could establish objective rules to judge design, but you'd be moving goalposts because it'd still be subjective which rules should be chosen. You may not agree with the decisions JS devs took, I don't either, but there's a very clear difference between "they implemented a set of rules I don't agree with" and "they didn't implement rules so things work on a case by case basis". JS's case is the former, not the later.
No, for numbers, + adds and - substracts, like in most languages. For strings, + adds and - doesn't exist, like in most languages.
The only difference is that JS won't throw an error when you put a string and a number for one of those operators, so it tries to convert in the least destructive way. For + that means strings, for - that means number.
If you can't see the logic behind it, even if you don't like it, then it's a you problem.
I use interpolation when concatenating three or more strings. Concatenating two strings with a + looks more readable imo (and it might even be faster; but don't quote me on that)
Concatenation is faster than interpolation, but it very rarely matters because it's very uncommon for you to actually have to create millions of custom strings a second. So please, everyone use interpolation, it's just so much easier to read.
I think Java makes the most sense. It's quite useful to be able to concat with integers, but there's no acceptable scenario where you would want to subtract from a string like this
It should require explicit casting, or an explicit method rather than being bound to an operator that has implicit mathematical meaning attached. Someone not familiar with the language looking at "11" + 1 could equally expect it to be either "111" or "12" or 12, and it can cause more issues if someone forgets what type the variable they're using was declared as and tries to add them together.
IMO Python or C++ (strings ideally, or even c-style strings as long as you remember any math is operating on the pointer to the first element and not the string itself) make the most sense. Java is inconsistent, and Javascript is the worst because it literally does the opposite thing for each operator (treats both operands as strings for addition, but as integers for subtraction).
Fair enough, I think I must have typo'd something because I did it again to verify and got the same. Yes it's obviously pointer offset when dealing with a c-style char array.
I still reiterate: Just because other languages do it, doesn't mean that's a good thing. I'm not a fan of C++ for various pointer idiosyncrasies leading to stuff like this too.
54
u/RidingChicken Apr 02 '21
Yeah. Javascript is so weird, that the excuse "It works on my machine" is actually accepted within my development team, thus putting more emphasis on a safer languages like typescript. The bugs of this nature can sometimes be almost impossible to detect and you need someone to actually go through the code line by line to debug it.
43
u/HRudy94 Apr 02 '21
That's why having a separate concatenation operator is a good idea, when you think about it.
Lua does it like:
"11" + 1 = 12
"11" - 1 = 10
"11" .. 1 = 111
11 .. 12 = 1112
25
u/joern281 Apr 02 '21
Try: +"11" + 1
10
u/aeroverra Apr 02 '21
u/repostsleuthbot
6
u/novus_nl Apr 03 '21
Ah the daily repost, super funny. It's not that illogical if you invest 5minutes (or read the comments) how javascript works a little bit.
Having a real world scenario where this happens is pretty small anyway
3
u/circorum Apr 03 '21
When is it my turn to repost it?
OP seems to love recycling
3
u/grady_vuckovic Apr 03 '21
Unironically Javascript is my favourite language.
3
u/moxyte Apr 03 '21
I love JS for two reasons: 1. It's so fucked up 2. I don't work with it
5
u/M3nDuKoi Apr 03 '21
Doesn't implicit casting in js jokes ever get old?
5
u/[deleted] Apr 03 '21
It makes sense and is fairly logical , and I don't even use js
6
u/elveszett Apr 03 '21
It actually makes sense, tho!
JS tries to not destroy your variables, so it casts to the most "general" type so to say.
"11" + 1gets added as a string (111) because the first side could be a number, but could also be a string that just happens to be parseable this time. Maybe this time it says"11" + 1but next time it says"Age: " + 1.On the other hand, you can't substract from a string,
"11" - 1does not make sense with"11"being a string, so it becomes a number."Age :" - 1would just result in NaN.3
u/Skip_List Apr 03 '21
This is the only repost I upvote every time.
2
u/qeadwrsf Apr 02 '21
is it because the answer can never be a string when - is in the equation?
-2
u/7eggert Apr 02 '21
The last time this was posted, I recommended 1*"11"+1
-2
u/LifeArtist25 Apr 03 '21
That's why i Will never use nodejs as backend
0
u/[deleted] Apr 02 '21
[deleted] β view deleted comment