In Javascript, is myObject == null a valid way to handle checking for undefined as well?

javascript

Solution

Yes, that's a nice way to check for both. The language specification states (regarding `==` for comparing values of different types):

2) If x is null and y is undefined, return true.

3) If x is undefined and y is null, return true.

Here x and y are the terms of a comparison `x == y`. When you're comparing `x == null`, it will only be true if `x` is `undefined`, or `null` itself.

Just to be clear, when we say "undefined" here we mean the value `undefined`, not variables that are not defined (those produce a ReferenceError whenever they're used, except with `typeof`).

And regarding WWDCD, I'll quote Ian: Crockford would suggest what JSLint suggests (obviously) because == is voodoo to him. That means "use `===`, never `==`". So this would be a non-question for him.

Problem

In Javascript, is `myObject == null` a valid way to handle checking for undefined as well as null? JSLint would prefer that I do `(myObject === undefined || myObject === null)` What Would Doug Crockford Do? (WWDCD)

Original source