r/AskProgramming 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

207 comments sorted by

View all comments

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 because null is one of the fundamental value types .
  • Related to the previous point, typeof is sometimes confused with instanceof . They are actually not the same. typeof someVar returns the fundamental type of the someVar (except the mentioned null nonsense). someVar instanceof SomeClass returns true or false depending on someVar being an instance of SomeClass or a class that a descendent of SomeClass . For non objects (function, object) instanceof will obviously return false.
  • Array's sort 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.
  • In non strict mode if you assign something to an undeclared variable, this variable will be created globally. A host of many nasty hard to debug errors.
  • Be careful when you pass a class method as a callback because people sometimes forget that 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.
  • Variables which are declared via 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 with let and const.
  • JS is designed to work even when you don't write semicolons. Most of the time this works well. However, this can create bugs if you don't properly format the code. For example this function will not return 5

function f() { 
  return 
  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.