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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

filter(Boolean) removes more than null and undefined: it also drops values such as 0, false, NaN, 0n, and the empty string. If those values are valid data, use values.filter(value => value != null) to remove only nullish entries.

Use a nullish predicate when only null and undefined should be removed

For example, this keeps zero while excluding both nullish values:

const counts: Array<number | null | undefined> = [0, 1, null, undefined];

const present = counts.filter(value => value != null);
// [0, 1]

In JavaScript and TypeScript, the loose comparison value != null intentionally matches both null and undefined. If you prefer to avoid loose equality, the equivalent explicit check is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const present = counts.filter(
  value => value !== null && value !== undefined
);

Choose the predicate according to what the data means. If an empty string, zero, or false is a valid value, do not use a truthiness filter to clean the array.

What filter(Boolean) removes at runtime

Array.prototype.filter calls its callback for each array element and retains the element when the callback result is truthy. Passing Boolean directly converts each value to a boolean, so the filter discards every value that converts to false—not just nullish ones. See the JavaScript Boolean reference for the conversion rules.

Falsy values include:

  • false
  • 0 and -0
  • 0n
  • NaN
  • "", the empty string
  • null
  • undefined

For instance, [0, 1, null].filter(Boolean) produces [1]. If zero represents a real count, that is data loss rather than null cleanup.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

How TypeScript’s type inference affects the result

Runtime filtering and static typing are related but separate. JavaScript determines which elements remain using truthiness. TypeScript analyzes the callback to decide whether it can narrow the resulting array’s element type.

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

TypeScript 5.5 introduced inferred type predicates for qualifying functions, including suitable callbacks passed to filter. Its 5.5 release notes demonstrate filtering out undefined and explain the limits of truthiness checks: “A truthiness check will infer a type predicate for object types, where there’s no ambiguity.” That qualification matters. With primitive unions, a truthiness check also excludes valid falsy values, so it does not express a general “not nullish” test.

Do not assume every boolean-returning callback is a type guard, or that type inference makes filter(Boolean) behave like a nullish check. The explicit comparison states the intended runtime condition and gives TypeScript a check it can analyze under its inference rules.

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

When truthiness filtering is appropriate

Truthiness filtering can be appropriate when every falsy value is meant to be discarded. For example, a collection of objects with optional nullish entries may be a suitable case because ordinary objects are truthy. But for a collection that can contain strings, numbers, or booleans, decide whether empty strings, zero, or false are meaningful before filtering by truthiness.

If you want a reusable nullish check, make its type predicate match its implementation:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
function isNonNullish<T>(value: T): value is NonNullable<T> {
  return value !== null && value !== undefined;
}

const present = values.filter(isNonNullish);

The TypeScript Handbook’s narrowing guide covers truthiness narrowing and explicit null checks. Its introduction describes the Handbook as a comprehensive guide to TypeScript features and behavior, rather than a complete language specification.

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.