avatarStephen Curtis

Summary

The provided content discusses the history, usage, and differences between null and undefined in JavaScript, as well as the peculiarity of typeof null returning 'object'.

Abstract

In JavaScript, null and undefined are two distinct values used to indicate the absence of a meaningful value in a variable. Undefined signifies that a variable has been declared but not yet assigned a value, or that a property does not exist within an object. On the other hand, null is used when a programmer intentionally sets a variable to a state signifying 'no value' or clears its current value. Despite their distinct purposes, both null and undefined are considered types in JavaScript with their own specific configurations of bits. An anomaly in JavaScript's typeof operator causes typeof null to incorrectly return 'object', which is considered a bug by the language's creator, Brendan Eich, although some argue it's due to null being treated as a null pointer reference.

Opinions

  • The author suggests that null and undefined serve as placeholders to inform the programmer about the state of a variable, with null being the intentional absence of any value and undefined indicating a variable that has not been initialized.
  • It is implied that programmers should only set variables to null and not undefined, except when interacting with APIs or libraries that specifically check for undefined.
  • The author posits that the behavior of typeof null returning 'object' is a bug, despite Brendan Eich's attribution of this behavior to a deliberate design choice to treat null as a null pointer reference.
  • The article concludes with a recommendation for an AI service, ZAI.chat, which is presented as a cost-effective alternative to ChatGPT Plus (GPT-4), offering similar performance and functionality.

A brief history of Null and Undefined in JavaScript

Null and undefined in JavaScript are actually values and types created to simulate errors and keywords common in other programming languages.

When a variable is `undefined`, or unitialized, in most programming languages it means that a space in memory has been assigned to a variable name, but the programmer has not yet done anything with that space in memory. This usually results in a compile time error.

When a variable is `null` in other programming languages, null is typically a keyword to indicate the space in memory is a pointer (reference), and that pointer is pointing to an invalid memory address (usually 0x0). This is usually used when a programmer is done using the value of a variable and wants to purposefully clear it by literally pointing it to nothing.

In JavaScript, `null` and `undefined` are values and types. Just like numbers and characters, `null` has a specific configuration of 1’s and 0’s that indicates it’s type is `null` and that it’s value is `null`. Same with `undefined`. These are used in JavaScript to act as placeholders to let the programmer know when a variable has no value.

What's the difference?

Undefined is supposed to mean a variable has no value (or a property does not exist) because the programmer has not yet assigned it a value (or created the property).

Null is supposed to signal that a variable has no value because the programmer purposefully cleared the value and set it to `null`.

So you should only ever set variables to `null`. Never to `undefined`.

...UNLESS you are working with existing code or a specific api or library explicitly checking for `undefined`, then of course to force the behavior you want you’d have to make an exception.

What about typeof?

But why does `typeof null === 'object’`? Well, all reference types (pointers) in JavaScript are objects. In early JavaScript, null was meant to simulate a null pointer (reference), ergo it was hard coded to return 'object' for it’s type. At least that’s my best guess, according to Brendan Eich it’s a bug. I think he just forgot.

JavaScript
Programming
Null
Undefined
Computer Science
Recommended from ReadMedium