Technically, undefined is a valid value for assigning a variable. It really matters, although this is usually not useful:
var foo = { bar: undefined }; foo.hasOwnProperty("bar"); // returns true foo.hasOwnProperty("bat"); // returns false
also:
for (var n in foo) { console.log(n, foo[n]); // should log "bar" and "undefined" }
I personally will follow the recommendations given by Crockford in this case. Assigning undefined as values can lead to code confusion and therefore should be considered bad. This surprises people not expecting this.
We already have null to use for this purpose, which less confuses future maintainers. Let undefined really mean that it is not defined, and use null to denote the unknown.
Remember that while reading Crockford's book, you read tips on best practices and opinions on how javascript should work in accordance with it. How javascript actually works is a completely different matter (and, according to him, not necessarily a good one).
source share