archives

« Bugzilla Issues Index

#3860 — Description of "purpose" for `undefined` and `null` values


4.3.10 undefined value
primitive value used when a variable has not been assigned a value

..

4.3.12 null value
primitive value that represents the intentional absence of any object value


---------

This is (I hope) a minor nitpick and one I've long wanted to have tweaked. My feedback here comes from teaching JavaScript and running across how these values are actually used in the real world, not only by those new to the language but those who've been steeped in habits for years.

I'd make the following observations about those above descriptions:

1. They seem "suggestive" rather than "required". I'm not sure if that's what's called "non-normative" or not, but I think I've gathered that is. Anyway, their tone doesn't seem to make any requirements of implementors, but rather just suggest (more to developers) who they *should* use the values. As such, it seems like a tweak, even at this late stage, might be possible.

2. I don't think these suggestions represent how most people think about the values, but more importantly they don't seem to map to how they're commonly used. I think we could/should tweak them to better fit.

3. Here's what I'd suggest as a tweaked wording for 4.3.10:

primitive value used when a variable or property does not currently have another value

Reasoning: of course `undefined` is used for both variables and properties. But more importantly, the current "not been assigned" implies on reading "not been assigned YET", which is a common misconception. A variable can have been set to a non-`undefined` value previously and now it's been set back to `undefined`.

4. Here's what I'd suggest as a tweaked wording for 4.3.12:

primitive value that represents the intentional absence of any other value

Reasoning: `null` is commonly used to "unset" any manner of values, from booleans to objects and everything in between. It's not an "object" value, and whereas it may have originally been intended to be the "non-object object value", that's not at all how it's commonly documented, used, or described today. So I just tweaked "object" to "other", that's it.

5. One last observation: most developers recommend, and indeed tend to do in their code, that `undefined` and `null` are treated as indistinguishable. The way they are `==` to each other and no other values makes that quite convenient, in that you can say `x == null` and it catches either `null` or `undefined`, and no other values.

------

While I've filed both of these suggestions as one issue for simplicity, I'd also encourage to consider these as separate suggestions if need be. That is to say, I'd welcome either of the two changes even if both don't get changed right now.


One other quick observation I forgot:

6. I've heard many times people make the claim that `typeof null == 'object'` is somehow intentional, to indicate that `null` is supposed to be an "empty object reference" value, and they further use the current "any object value" wording to support that claim. I believe that's quite inaccurate, at least as far as JS stands today, as Brendan Eich himself has said that `typeof null == 'object'` is just a bug, not an intentional feature or signal of how to use it.

I believe changing "object" to "other" in 4.3.12 would sever ties with such misconstrued reasoning once and for all.