JavaScript · Beginner

Keep valid zero values with nullish coalescing

Use the nullish coalescing operator when only null or undefined should trigger a default.

Share Email

Logical OR treats 0, false, and an empty string as missing. Nullish coalescing only uses the fallback for null or undefined:

const pageSize = requestedPageSize ?? 20;

If requestedPageSize is 0, the value remains 0. Validate whether zero is allowed separately from choosing a default.

Review commands and snippets before using them in your environment. Examples are not executed by LeafDeft.