A Dirty Secret of JavaScript: Creators Don’t Want You to Know!

Kapil Pant
3 min readFeb 13, 2021

--

A well discussed, and proven fact of JavaScript that will make you go haywire.

Eastern lowland gorillas have a strong skeleton with a big skull compared to the size of the rest of their body, a short snout and a jawbone with teeth.

Brendan Eich created JavaScript for the first time in September, 1995. It was a shock for people to know that he took only 10 days to develop the scripting language, then known as Mocha. Later, the scripting language was named JavaScript, and many people believed it to be a conspiracy, the team used to market their newly developed scripting language by using the name of the most popular language of that time (Java), regardless of the fact that JavaScript and Java have almost nothing to do with each other.

But since then, there has been a dirty secret about which, many people are not aware of. You all might be using JavaScript for many years for building websites. Although there might be people who are using it as a competitive programming language, but the basics of JavaScript remain same for all.

What could be the secret?

Before I directly jump on to the secret, I would like you to answer a very simple question.

What is the difference between “null” and “undefined”?

After researching and scratching your heads over this vexing question, you’ll come up with an answer which is somewhat similar to this: “null” means empty, and it states that the variable which is assigned a value as “null”, will contain nothing, and it won’t use any memory. On the contrary, “undefined” is, as the name suggests, something which is not defined. So, if you try to print the value of a variable which is not defined, you’ll get the output as: “undefined”.

So, if you agree to this answer, you’ll have to agree to the fact that “null” and “undefined” aren’t similar to each other by any means, but if you try to compare each other, you’ll see something unusual.

null == undefined;
Output for the above code snippet

Truth of paramount importance

There have been a lot of discussions and arguments based on “null” and “undefined”. Many have even stated that both “null” and “undefined” return “false”(a Boolean value), and as a result, “null==undefined” is equivalent to “true”(a Boolean value), just because “false==false” will return “true”. But, it isn’t true. The entire team of creators, during the developing stage of JavaScript, made some pre-defined rules for execution. But as there are exceptions, they deliberately confined the result of the above discussed problem to be “true” just because they did not want the entire language to suffer due to a single piece of code. According to the entire logic and rules of the language, it would have been very difficult for the creators to give it a particular and logical output. So, in order to prevent the internet from breaking, they decided to restrict the output of the problem to be as “true”.

Bonus code snippets for you to try and get perplexed!

!null;
null == false;
null == true;
null*25; // null*(any number)
!undefined;
undefined == false;
undefined == true;
null === undefined;
!null === !undefined;

Summary

In this article we got puzzled by one of the most important and confusing facts related to JavaScript, which happens to be one of the most favourite questions of the interviewer. At last I have provided you with some code snippets similar to the above problem for you to execute, print output, and wonder why?

--

--

No responses yet