As mentioned in the topic JS the Good Parts seems to claim that a property value can't be undefined. However if you do the following in Chrome's console for example:
var foo = {}
foo.bar = undefined
foo
Then click to expand the foo object you can still see that foo contains a property called bar with the value undefined. Of course from the Javascript side you can't tell the difference between foo.bar returning undefined and foo.unexistingproperty returning undefined. But what is the point of the console still clinging to the property that was set to undefined?
As mentioned in the topic JS the Good Parts seems to claim that a property value can't be undefined. However if you do the following in Chrome's console for example:
var foo = {}
foo.bar = undefined
foo
Then click to expand the foo object you can still see that foo contains a property called bar with the value undefined. Of course from the Javascript side you can't tell the difference between foo.bar returning undefined and foo.unexistingproperty returning undefined. But what is the point of the console still clinging to the property that was set to undefined?
Share Improve this question asked Dec 10, 2010 at 10:14 SamSam 15.4k6 gold badges28 silver badges40 bronze badges 6- 1 I think you could call it a fairly useless Chrome 'feature' :) – sje397 Commented Dec 10, 2010 at 10:18
- 3 @sje397: Not useless at all. There's a difference between a property existing and being undefined and a property not existing. See my answer. – Tim Down Commented Dec 10, 2010 at 10:27
- So like a lot of answers said I know that you can use delete to get rid of the property and so on. But the main idea with my question was why the book claims the value can't be undefined. Clearly this seems to be an incorrect statement by Crockford. – Sam Commented Dec 10, 2010 at 10:37
-
@Sam: I don't have the book to hand. Could you quote the relevant part from it? I suspect he may simply be remending against assigning
undefined
to a property, which is not unreasonable advice. – Tim Down Commented Dec 10, 2010 at 14:11 - No the formulation in the topic is an actual quote from the book. It's not worded in the way that most of the remendations in the book where Crockford first mentions that something can be done, but remends it. The text is from Chapter 3 Objects and does say that "A property value can be any value except for undefined". – Sam Commented Dec 10, 2010 at 14:55
3 Answers
Reset to default 7There's a difference between a property existing and being undefined and a property not existing, so Chrome is being sensible here. If you assign a value to a property then the property fundamentally exists in that object. One difference is that a property that has been explicitly set to be undefined will show up in a for...in
loop:
var foo = {};
foo.bar = undefined;
// The following will alert "bar"
for (var i in foo) {
alert(i);
}
Another is that "bar" in foo
will return true
, as will foo.hasOwnProperty("bar")
.
Technically, undefined is a valid value to assign to a variable. It does make a difference albeit 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 would follow the advise given by Crockford in this case. Assigning undefined
as a value can lead to confusing code and should therefore be considered bad. It surprises people not expecting it.
We already have null
to use for this purpose which is less confusing to future maintainers. Let undefined
really mean not defined and use null
to mean not known.
Remember that when reading Crockford's book you're reading advice on best practices and opinion on how javascript should work according to him. How javascript actually works is a different matter altogether (and according to him not necessarily good).
undefined
is Javascript's way of telling you that a value doesn't exist instead of throwing an error when you access the property without a try/catch block. Although technically what Chrome is displaying is wrong by Crockford's account, it is at least logical; the property exists, albeit as undefined
.
When you want to remove a property, you can use the delete
operator:
delete foo.bar;