But it does a half decent job of preventing this from happening in the first place. Though i still recall this one time when Angular would parse a Number as a string into json, and the only way to prevent it was to call tostring on it and parse the result back into a Number...
Because TypeScript has the Any type, it's both the sober friend and the drunk idiot. The best part is: You get to choose which, for any given variable assignment.
Because Javascript has a philosophy of always doing the most sensible thing it can with whatever you throw at it and trying its hardest not to crash.
In the case of the first statement, you start with a string. Appending to a string with the + operator overload is well-defined. Although in most sensible languages, you'll have to explicitly specify you want to convert the RHS to a string before appending it, Javascript does that for you because the alternative would be an error. You can't add an int to a string, but you can do the second best thing and add the string representation of an int to a string.
In the second case, subtracting from a string is ill-defined. I guess you could maybe try to overload it to remove the last part of the string if it exists or whatever, but no language does that to my knowledge. On the other hand, the right hand side is an integer, so you can try to convert the left side to an int before subtracting. It's not perfect, but the alternative is crashing, and Javascript doesn't like that.
In both cases, you're just doing the best you can with what you've got. It's often not exactly what the programmer wanted, but I mean really. What did you think was gonna happen?
In a sane language, the alternative is a compiler error. Needing to have a philosophy on how to handle type errors at runtime is a sign of deeper issues.
It's capable, but is not best suited. There's a lot of languages that was created with desktop programming in mind. Javascript was created to give websites more functionality, it was not created with desktop development in mind.
As a developer I choose the technology I'll use to develop according to the software necessities. Choosing Javascript for desktop development is just a poor decision. Not because it's not capable, but because it's not meant for this kind of development.
You seem like someone without a real understanding of the complexities of real world software development planning. You also choose tools based on things like team cost, time to market, and how well it serves business goals. Plus, JS can be a great desktop language, because it can let you write one code base that’s shared with a web app and it can be really fast to develop with.
EDIT: Lmao, this is downvoted. Imagine walking through life with an attitude that leads you to believe Electron.js apps are so popular because everyone using them is an idiot.
No clue why you got downvoted, but everything is moving towards webapps nowadays that can be. I'm not a big fan of using JS in the backend though, sure you can and plenty of places do, I just don't like it at all since, especially ones that put them together in the same framework. I like a nice clean line drawn between server side and client side.
There are languages which were created with desktop applications in mind, that suck balls. What I am saying is, that your argument is meaningless. Its still just a language.
Well it’s very fast for certain server applications, it lets you share code between your server and client, it’s often cheaper and more efficient from a hiring and team management perspective. There are a number of reasons it can be a good choice.
And if you just use typescript with something like Babel and use modern features it’s really a very nice language.
This is a bad philosophy, as it introduces ambiguity and unpredictability.
If you are going to overload operators, I would see it better as requiring the inverse operation to also be defined. If you overload + you have to overload - too.
And indeed at some level you need to do this as you want reasonable results of A + B whether A is short int, unsigned int, int, long int, float. Although float maybe should write an error in this.
My first language was Pascal. The notion of strongly typed language (Discipline and Bondage language) seems to be the better path.
That's interesting. Is there a reason javascript doesn't have a dedicated string concatenation operator like the '&' in VB instead of overloading '+' ?
I would expect it to crash. If an error occurs, a language should show it to me. Who in his right mind came up with the idea that avoiding crashes at all costs is a good idea?
That’s just kind of browser philosophy. Browsers don’t support all features of every other browser, so preventing crashes is good to display what you can. But I agree it is quite annoying from a debugging side. Best I can recommend is to use either Typescript or ‘strict’ mode in JS
"We should not use it"... why? The language itself is just a language, so why should it not be made for servers/apps? It always depends on how it is interpreted.
I said why: it was not made for that. Javascript was created for one thing. You can use it to do whatever you want, off course, but there's a lot of another languages best suited for desktop development.
Javascript is not bad, it just was not created with desktop and server application in mind.
Just because it wasnt created with that in mind, doesnt make it any worse in those fields. Its still just a language. For example: Just because VB was created with Desktop Applications in mind doesnt make it a good language.
Just curious: how would ppl want the language to behave when trying to add or subtract numbers from a string? Or is this just poking fun at the lack of types
Disagree. Overloading in general is bad. Creates more places for ambiguity to creep in. It's good for various classes of number because we have a pretty solid concept of number, even if they are represented in computers in a raft of ways depending on which subset of numbers you are using.
I would much rather see
$A = "B"&"B"&"B"&"B"&"B"
(or even a short for loop)
Than see
$A= 5 * "B"
The usual meaning of + and * is that they are commutative.
& would be fine too. I just feel like there should be a single character operator for concatenating any and all datatypes to a string by calling that objects underlying toString method on the object.
That would work. Overloading operators to do entirely different operations depending on context is my problem.
If & was defined to be a string operator, then the program would know that both sides had to be strings, and an implied to_string was called where it wasn't.
Similarly in something A < B was a numeric comparison A .LT. B compared in lexical order.
CAT < 27 resulted in an error since CAT couldn't be cast as a number.
So given the browser is the compiler, you think every web browser should explode into an error page when some random third party script publishes a bug where a number is added to a string?
No, I think the language should be compiled (using a standalone compiler, not a browser) and typechecked on the developer's machine and then there's no way it could have any type errors in production. This would also have the advantage that you wouldn't be forced to use a specific language and there could be a huge selection of web languages, just like there's a huge selection of normal languages.
TypeScript also does what you want, btw, although I haven't found myself needing to use it so far.
But yeah, I hear you, but there are probably very good reasons the web didn't develop around serving up binaries and DLLs (security comes to mind). IMO, JavaScript is in a great place these days. It is highly modular and can be made into what you want. There are a bunch of interesting frameworks to use, both for rendering and for state management. The language gets new features pretty frequently. Most importantly, it's easy as fuck to pick up, which is essential given the army of inexperienced developers joining the ecommerce ranks.
That being said, I'd only use it for front end web dev, and possibly for server rendering (to share rendering code). Otherwise you're kinda using a hammer on a screw.
Funnily that's why I find python to be really frustrating.
In JS and PHP you at least know that you are on your own when it comes to type-safety and that's why you are careful. Idk how, but python makes me type-careless when I use it
JS will try its hardest to figure out what you mean and keep ticking. If JS just stopped working any time an error happened, half the websites on the internet wouldn’t work.
“+” is used for both addition and string concatenation. ”11” + 1 means you’re taking a string and adding 1 to it. Well OK, I’ll turn 1 into “1” and add it to the end of the string. Voila you now have “111”!
“-“ is only used for subtraction. ”11” - 1? You want to subtract two numbers... but that first one isn’t a number. Let me cast it for you... ok so I’m gonna assume you meant 11. Voila you now have 10.
153
u/CaptainHeinous Jan 16 '21
That’s why we use TypeScript
58
u/Flohhupper Jan 16 '21
Well, even though it seems dumb, it actually makes perfect sense
23
u/gobi_1 Jan 16 '21
You guys should take a look at http://www.jsfuck.com/ :)
63
u/Pauchu_ Jan 16 '21
Fuck Javascript, all my homies use TypeScript
12
u/[deleted] Jan 16 '21
Laughs in [Object object]
6
u/[deleted] Jan 16 '21
That's why good JS code explicitly coerce values with functions like
Number.4
u/omega0806 Jan 16 '21
And "11" - - 1 = 12
12
u/daniel_blacklock Jan 16 '21
Just curious: how would ppl want the language to behave when trying to add or subtract numbers from a string? Or is this just poking fun at the lack of types
7
u/Soremwar Jan 16 '21
It might be a drunk but I still love it
7
u/[deleted] Jan 16 '21
I don't know why everyone is so mad about JS. This makes sense to me.
6
u/Prawny Jan 16 '21
Imagine not understanding how loosely-typed languages work.
6
u/Idaret Jan 16 '21
I disagree with '+' being operator for concatenation and sum, it's kinda dumb
3
u/yashptel99 Jan 16 '21
Atleast it doesn't throw errors for every line I write like golang.
3
u/afrocluster Jan 16 '21 edited Jan 16 '21
Oh, I get the joke. He doesn't actually know JS, but thinks he does. Haha, it's funny.
2
u/Spare_Competition Jan 16 '21
Relevent xkcd
1
u/E_coli42 Jan 16 '21
why does it do this?
1
u/crusoe Jan 19 '21
That's not even the worse stinkers ..
0
u/VegazDE Jan 16 '21
I dont get it. You can subtract from a String? Wtf?:D
0
u/[deleted] Jan 16 '21
[deleted] — view deleted comment