One thing I have been doing for quite a while is using equality to (==) undefined or null to check if something is equal to one of those two values. Unfortunately, if your JS code needs to pass a linter, this type of check is not acceptable. On the other hand, it is important to note that the following three equality checks all result in true:

Therefore, the main reason linters make sure you use strict equality (===) when checking for these values is because it is assumed that you only want to check for one or the other. Notice that if we do same test as above, but switch out normal equality for strict equality, the first comparison results in false:

In conclusion, if your goal is to see if a variable is null or undefined you can use standard equality (==) against one of these values. If you need to know if it is specifically equal to one of these values, you should use strict equality (===). You can find more information from Mozilla here.

Categories: BlogJavaScriptJScript

Leave a Reply

Your email address will not be published. Required fields are marked *