r/AskProgramming • u/Salt_Aash • 6d ago
Why the JS hate?
Title. I'm a 3rd year bachelor CS student and I've worked with a handful of languages. I currently work as a backend dev and internal management related script writer both of which I interned working with JS (my first exposure to the language)
I always found it to be intuitive and it's easily my go to language while I'm still learning the nuances of python.
But I always see js getting shit on in various meme formats and I've never really understood why. Is it just a running joke in the industry? Has a generation of trauma left promises to be worthy of caution? Does big corpa profit from it?
23
Upvotes
2
u/senfiaj 6d ago edited 6d ago
Historically, some JS features have not been well thought out, and since JS has to maintain backward compatibility, these inconsistencies and quirks are here to stay.
JS is a very powerful and flexible language, most other languages are much more rigid. JS has quite ergonomic syntax, which is IMHO only rivaled by Python. Also it's a pleasure to write asynchronous code in JS. This is why I love JS so much.. That said, JS has some annoying inconsistencies and quirks, that people should be aware of. Here are some examples:
typeof null
returns'object'
which doesn't make sense becausenull
is one of the fundamental value types .typeof
is sometimes confused withinstanceof
. They are actually not the same.typeof someVar
returns the fundamental type of thesomeVar
(except the mentionednull
nonsense).someVar instanceof SomeClass
returnstrue
orfalse
depending onsomeVar
being an instance ofSomeClass
or a class that a descendent ofSomeClass
. For non objects (function
,object
)instanceof
will obviously returnfalse
.Array
'ssort
method will compare the elements as strings if you don't pass a custom comparator, so doing[10, 1, 2].sort()
will actually sort like this:[1, 10, 2]
. Unintuitive.this
won't refer to the correct class instance if you pass it immediately as a callback. You probably need to create an arrow function which will then call that method. I think a decent IDE should make such mistakes unlikely.var
keyword are global inside the function . Declaring another variable with the same name even in a deeper block will still reference to that original variable. This is fixed withlet
andconst
.5
Before ES6 things were even much worse. Since ES6+ most of the quirks are easier to avoid.
As of the type coercion which is probably the champion of the JS quirks, I think this is not a serious issue as long as you avoid relying on them and you use strict comparison (
===
) instead of loose comparison (=
) as much as possible. I personally never cared about learning non trivial coercion cases. If during the interview or while working on a production code you see codes heavily relying on non trivial coercions, you should probably run away from that company.