I got nothin’ here folks. Sorry! I have no idea what I’m gonna write about for this, the sixth issue of the JavaScript for Everyone newsletter. Thanks for signing up anyway, though.
Listen, I’ve been busy! The mental back-burner that normally keeps newsletter ideas at “the barest suggestion of a simmer” (to quote Cantabrigian hometown hero Julia Child) has been fully occupied for the past couple weeks. So yeah, no. Sorry again. Drawing a blank. Nada. Null— well, that's not quite right. Not null. undefined
, sure, but not null
, strictly speaking.
It’s just that null
represents a defined absence of value (of “object value,” technically — long story). null
is explicit non-value.
undefined
is something else entirely (but still not something). undefined
represents the implicit absence of value: where a value could exist but none does, or is expected to result but hasn’t yet, that value is undefined
. It’s not that there will be nothing in this newsletter, it’s just that there's nothing of value here! Thanks again for signing up, by the way.
Here, look: this isn’t a sentence I trot out often, but a quick way to square the concept of undefined
versus null
is with a little math:
undefined * 2;
// result: NaN
null * 2;
// result: 0
What is the result of doubling something that could be anything but could also be nothing? You’re in “what is the sound of one hand clapping” turf. JavaScript is no philosopher, but given that the answer isn't gonna be, like, eight, the best it can do is NaN
. On the other hand, nothing — explicit nothing — multiplied by two? Well, there’s a number to represent nothing, and two of that is also nothing. The answer is 0
.
Fact is, I don't find myself working with null
values much, unless I’m either (grudgingly) following a convention established by the project I'm working on, or scowling at a DOM method that should really have been designed to give me an empty nodeList instead of null
but do not get me started on this. No, for my money, there's really only one time you should be assigning a null
value: when you’re working with JSON.
A JSON value can be an object, array, number, string, true, false, or null.
undefined
isn’t part of the JSON syntax, which makes sense where JSON represents a set of defined values. There would be no point in making a property undefined
as you’re defining it, when nothing in that JSON file could define it.
However, you will sometimes need to be able to define a property as having no value at all. For example, if a JSON has a property for a user’s middle name and a user doesn’t have one, that doesn't mean their middle name is an empty string — the value of that property should be explicitly nothing:
{
"newsletterSubscribers": [
{
"name": {
"fore": "Miku",
"middle": null,
"sur": "Hatsune"
},
"title": "Creator of JavaScript"
}
]
}
So hey, maybe I’ve got nothing to write about in this newsletter, but at least it isn't null
. I mean, I like to think of this newsletter as a store of potential value. If I sent it out completely blank, sure, yeah, there’s a case to be made that I’ve given it a null
value, where it would be explicitly nothing. But until I either come up with something to write about, or send out nothing at all, this newsletter has an undefined
value. That might not be anything, but hey: it’s not nothin’.
Course draft statistics
- Emdash count: 1,365
- Discrete references to the handshake from the beginning of Predator (1987): 3
- Author's score on new Date("wtf"): 17/28