It is 20:30 (8:30 PM) on Sunday, the fourth of January in the year of our Lord 2026, and I am…
Looking up whether I want to use `null` or `undefined` in my JavaScript code.
We party hard here.
@mahryekuh Once you've decided that, you'll get to um and ah over two equals signs or three!
@jscholes I usually use three equals signs, especially when running linters in the IDE.
Fun fact, I am refactoring now because I use empty strings in some places as I would in Django projects.
With empty strings, you cannot use null-coalescing operators, as those only trigger on null and undefined.
@mahryekuh the fact that these are two separate values in JavaScript is one of the decisions of all time
@kasdeya I have mixed feelings on it.
Practically, it feels inconvenient because these values do the same thing, and that's fine until someone mixes them up and does type-specific checks.
Semantically, I do like the option to indicate if the null-ish value is undefined on purpose. Undefined feels more like a "ToDo" whereas null feels like an intentional "empty" value.
The semantics are theoretically great, but only when everyone plays by the same definition and rules in a project.
@mahryekuh As someone who is primarily a backend dev, I've had this problem, and have seen many devs have problems fully understanding this concept. We are so used to seeing null jand understanding null, but then when we have to work with frontends we see the undefined and don't always realize that something can be undefined but it isn't null. It makes your head spin 🙃
@jrsofty Yep, agreed!
I posted my thoughts on this in another subthread:
Certainly a problem with the semantics between frontend and backend development.
In my own personal development work, I do my best never to intentionally return an empty value (aka a null) as in backend it can cause so many problems. I prefer to return an empty object. It's not null but you can tell it isn't filled in.
@jrsofty @kasdeya I rather like "null" as an intentionally empty value.
I think it's good to realize that the different frameworks, languages, and areas have different standards.
For example, I am used to using empty strings instead of null for strings. This is a remnant of how Django CharFields handle empty values.
I brought this logic to JavaScript out of habit, but it's super inconvenient. For example, null-coalescing operators only work on "null" and "undefined", which led to the events that prompted me to post the original toot.
Moral of the story: do not apply the same rules to different frameworks and different languages.
@mahryekuh a nice feature of == is that it coerces undefined to null. So
if (myVariable == null) {
// runs if myVariable is undefined or null
}
Picked up this from a colleague, and it is the only usage of == we permit in our code bases.
@mahryekuh Brendan Eich, the creator of JavaScript, supported California's "proposition 8" against gay marriage. When this became known, it sparked protests and controversy within Mozilla. When he was later selected CEO he had to resign after a few days (AFAIK this led to him initiating the Brave web browser).
Also led to this joke:
"Well, we knew from JS's == and === that Eich always had problems with equality". 😂