This interesting thing came up with one of our libraries that a colleague found out when he got stuck. I am just here to amplify and talk about it.
The Problem:
The issue was related to the server-side rendering of a react application. When attempting to render a page on the server side, one of our internal libraries kept giving a "not defined" error on the window object causing a failure in the server-side rendering of a page.
This was the code that caused the "not defined" error:
Now, before getting into what's wrong with the above code, we need to understand the difference between "undefined" and "not defined" in Javascript.
"undefined" vs "not defined"
When a variable is declared but is not initialised to any value, then by default it holds "undefined" as the value. This "undefined" is one of the primitive data types in Javascript that is used as a default value for a declared but not initialised variable. If a variable has "undefined" as its value, it doesn't throw an error upon usage.
Whereas, "not defined" means that the variable isn't declared at all and that Javascript couldn't find its existence. This scenario always leads to the error seen in the above code sample.
Summarizing this as a code sample:
So, that is why the internal library threw a ReferenceError for the window object.
Better Approach:
This approach is based on how my colleague fixed it and a few things I thought of. This is where we have to think about which is better in Javascript. "null" or "undefined"?
The solution to this issue was to change the if condition to do a typeof check like this:
This brings us down to the debate of "null" vs "undefined"
"null" vs "undefined"
I am standing at the "undefined" side here because, in JavaScript, when an object is declared, by default it takes undefined as the value and not null. Similarly, if there's an object with some attributes and one of the attributes is removed by doing a delete operation, the value becomes undefined and not null.
Thus, null is a value that has to be explicitly set and checked while undefined is the default way of doing it in JavaScript. Additionally, while typeof undefined is "undefined" while typeof null is "object".
A code example of all of the above:
Here's a screenshot from the Browser console for the above code:
We cannot do a typeof check on a variable that holds null because you wouldn't know if the object is a null object or a valid object.
And finally, apart from the not defined issue, why are typeof checks better than doing a plain check like the one in the problem section? The condition won't be proper if the value becomes a number and the assigned number is 0. Let us look at the example for the same:
In this scenario, the aim of our function is to simply check if the variable is initialised or not. While the variable x passed correctly, y failed even though it was initialised. This is because of the obvious that in a conditional statement, JavaScript considers 1 as true and 0 as false.
Therefore, changing the condition with a typeof check would give the expected result:
Henceforth, I rest my case with a conclusion that
- undefined is better than null.
- Do typeof checks rather than plain checks
Comments
Post a Comment