When To Check For Undefined And When To Check For Null
Solution 1:
Functions implicitly return undefined
. Undefined keys in arrays are undefined
. Undefined attributes in objects are undefined
.
function foo () {
};
var bar = [];
var baz = {};
//foo() === undefined && bar[100] === undefined && baz.something === undefined
document.getElementById
returns null
if no elements are found.
var el = document.getElementById("foo");
// el === null || el instanceof HTMLElement
You should never have to check for undefined
or null
(unless you're aggregating data from both a source that may return null, and a source which may return undefined).
I recommend you avoid null
; use undefined
.
Solution 2:
Some DOM methods return null
. All properties of an object that have not been set return undefined
when you attempt to access them, including properties of an Array
. A function with no return
statement implicitly returns undefined
.
I would suggest making sure you know exactly what values are possible for the variable or property you're testing and testing for these values explicitly and with confidence. For testing null, use foo === null
. For testing for undefined
, I would recommend using typeof foo == "undefined"
in most situations, because undefined
(unlike null
) is not a reserved word and is instead a simple property of the global object that may be altered, and also for other reasons I wrote about recently here: variable === undefined vs. typeof variable === "undefined"
Solution 3:
The difference between null
and undefined
is that null
is itself a value and has to be assigned. It's not the default. A brand new variable with no value assigned to it is undefined
.
var x;
// value undefined - NOT null.
x = null;
// value null - NOT undefined.
Solution 4:
I think it's interesting to note that, when Windows was first written, it didn't do a lot of checks for invalid/NULL pointers. Afterall, no programmer would be dumb enough to pass NULL where a valid string was needed. And testing for NULL just makes the code larger and slower.
The result was that many UAEs were due to errors in client programs, but all the heat went to Microsoft. Since then, Microsoft has changed Windows to pretty much check every argument for NULL.
I think the lesson is that, unless you are really sure an argument will always be valid, it's probably worth verifying that it is. Of course, Windows is used by a lot of programmers while your function may only be used by you. So that certainly factors in regarding how likely an invalid argument is.
In languages like C and C++, you can use ASSERTs and I use them ALL the time when using these languages. These are statements that verify certain conditions that you never expect to happen. During debugging, you can test that, in fact, they never do. Then when you do a release build these statements are not included in the compiled code. In some ways, this seems like the best of both worlds to me.
Solution 5:
If you call a function with no explicit return then it implicitly returns undefined. So if I have a function that needs to say that it did its task and there is nothing result, e.g. a XMLHTTPRequest that returned nothing when you normally expect that there would be something (like a database call), then I would explicitly return null.
Post a Comment for "When To Check For Undefined And When To Check For Null"