Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In JavaScript, 0 is a number; null and undefined are distinct values that represent absence in different ways. All three are falsy, so a simple truthiness check can mistakenly treat a valid zero as missing.

What do 0, null, and undefined mean?

Value JavaScript type Typical meaning
0 number A numeric value, such as a count, measurement, index, or calculation result.
undefined undefined A value that has not been assigned or found, such as an uninitialized variable or missing object property.
null object from typeof, though it is a primitive A value used to explicitly represent the absence of an object or value.

For example, a variable declared without an initializer has the value undefined. Reading a missing object property also produces undefined, and a function that does not return a value returns undefined. By contrast, null is commonly assigned deliberately to indicate an empty or unavailable object reference. For instance, document.querySelector() returns null when it finds no matching element. See MDN’s references for undefined and null.

Are 0 and null the same?

No. Zero is numeric data, while null is an absence marker. Neither strict comparison nor loose comparison treats zero as equal to either nullish value:

0 === null       // false
0 === undefined  // false
0 == null        // false
0 == undefined   // false

Loose equality does have a special rule for the two absence values: null == undefined is true, while null === undefined is false. Prefer strict equality (===) for predictable comparisons; loose equality can convert types in other comparisons. MDN explains the differences in its guide to equality comparisons and sameness.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why can a condition confuse zero with a missing value?

JavaScript treats 0, null, and undefined as falsy. So do false, the empty string (""), and NaN. A condition such as if (!value) therefore matches all of them; it does not tell you whether value is zero or absent.

const count = 0;

if (!count) {
  // Runs, even though count is a valid number.
}

Use a truthiness check only when every falsy value should count as empty. If zero is meaningful, check for the specific value or values your code considers absent.

How should you check for null or undefined?

Check specifically for null

if (value === null) {
  // value is exactly null
}

Check specifically for undefined

if (value === undefined) {
  // value is exactly undefined
}

This direct check applies when value is a declared identifier. Accessing an undeclared identifier normally throws a ReferenceError. For a possibly undeclared name, typeof name === "undefined" avoids that error; however, accessing a lexical binding before its declaration is initialized, while it is in the temporal dead zone, can still throw. MDN covers these cases in its undefined reference and typeof reference.

Check for either nullish value

if (value == null) {
  // Matches null or undefined, but not 0, false, or ""
}

This is a deliberate use of loose equality: value == null matches both null and undefined, but not zero. Use it when either nullish value should be treated the same; otherwise, use the corresponding strict comparison.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What does typeof return for these values?

typeof 0          // "number"
typeof undefined  // "undefined"
typeof null       // "object"

The last result is a historical compatibility artifact. null is a primitive, despite typeof null returning "object". Consequently, typeof value === "object" alone does not establish that a value is a non-null object. MDN documents this quirk in its typeof reference and its guide to JavaScript data types and data structures.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do defaults handle zero?

The choice between || and ?? matters when a default should replace missing data but preserve a valid zero:

const withOr = value || 10;
const withNullish = value ?? 10;

withOr;       // 10 when value is 0
withNullish;  // 0 when value is 0

|| uses the fallback for any falsy value, including 0, false, and "". Nullish coalescing (??) uses the fallback only for null or undefined. Optional chaining (?.) likewise treats only null and undefined as nullish when accessing a property or calling a method; it does not make zero equivalent to absence.

Quick rule for choosing a check

  • Use value === null when only explicit null should match.
  • Use value === undefined when only an undefined value should match.
  • Use value == null when you intentionally want either nullish value, but not zero.
  • Use if (!value) only when zero, false, an empty string, and other falsy values should all count as empty.
  • Use value ?? fallback to default nullish values while keeping zero.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.