0

I was reading a article about front-end development on Medium, when I stumbled upon an interesting piece of information, which is as follows:

The type of a variable can be determined by using the typeof(var_name) function. One thing to note is that typeof(null) returns object. This is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!

Why so? What kind bugs & code reliability this section is referring to?

Link for article : https://medium.com/@shoaibkhan_31475/cracking-the-front-end-developer-interview-part-1-be3619b60cc4

2

Making breaking changes to JavaScript has serious consequences. There are millions of sites out there which run code that may have been written years and years ago, and will possibly never be touched again.

This means, if we change the way JavaScript works, but those sites rely on the way it used to work, parts of the internet will flat-out break -- the code that drives them no longer works as expected.

On top of that, even if the standards committee decides to change the specification accordingly, that doesn't magically 'fix' this issue. There's also the browser vendors which have to actually implement the specification -- Chrome's V8 engine, Firefox' SpiderMonkey engine etc. They also have a strong incentive to not break stuff, since, well, users would just switch to a different browser if theirs isn't able to display the sites they go to anymore.

For a related, pretty recent, and mildly entertaining topic, see also the discussion whether the JavaScript flatMap function should be called smoosh, for very similar reasons.

  • 3
    If anyone is relying on the behavior typeof(null) === object, I suspect they have larger problems. – Robert Harvey May 18 '18 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.