Thanks for making the point. :-)
The only case when I use == is comparing to null, that also checks for undefined. It is an extremely rare when you need to check for undefined and null independently
I prefer Object.is() to compare NaN and for undefined I just pass variable inside of condition because undefined is falsey value.
If you need to check for undefined and null just do
undefined
null
if (variable === undefined || variable === null) {
if I see code doing:
if (variable == null) {
I have no way to know if you actually wanted to check only for null or null and undefined.
It is always better to be explicit.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for making the point. :-)
The only case when I use == is comparing to null, that also checks for undefined. It is an extremely rare when you need to check for undefined and null independently
I prefer Object.is() to compare NaN and for undefined I just pass variable inside of condition because undefined is falsey value.
If you need to check for
undefined
andnull
just doif (variable === undefined || variable === null) {
if I see code doing:
if (variable == null) {
I have no way to know if you actually wanted to check only for
null
ornull
andundefined
.It is always better to be explicit.