null and undefined

suggest change

Both values represent absence, but they are used differently:

They have different types and are not strictly equal:

typeof undefined; // 'undefined'
typeof null;      // 'object' — a historical language quirk

null === undefined; // false
null == undefined;  // true; this is one of the few deliberate uses of loose equality

Prefer strict equality for ordinary comparisons. A check such as value == null is sometimes used intentionally when both null and undefined should match; make that convention explicit in the surrounding code.

Both values are falsy, but so are false, 0, -0, 0n, NaN, and the empty string. Do not use a general truthiness check when those are valid values.

Defaults with ??

The nullish coalescing operator uses its right operand only when the left operand is null or undefined:

const configuredCount = 0;
const count = configuredCount ?? 10; // 0

In contrast, configuredCount || 10 would produce 10 because 0 is falsy.

Optional chaining stops a property access or call when the value to its left is nullish:

const city = user.profile?.address?.city ?? 'Unknown';
const result = callback?.();

Optional chaining does not suppress errors from an undeclared root identifier, and it should not replace validation when a missing value indicates invalid program state.

To test whether an identifier might be undeclared, use typeof name === 'undefined'. For an existing object property, use Object.hasOwn(object, key) when ownership matters; comparing a property with undefined cannot distinguish a missing property from one explicitly set to undefined.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents