Recommended Free Tools
filter(Boolean) removes more than null and undefined: it also drops 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 check when you mean “not null or undefined”
For an array where zero, false, or an empty string must be preserved, filter with an explicit nullish condition:
As an Amazon Associate I earn from qualifying purchases.
const present = values.filter(value => value != null);
In JavaScript and TypeScript, value != null matches both null and undefined. If your codebase avoids loose equality, the equivalent explicit form is:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →const present = values.filter(
value => value !== null && value !== undefined
);
For reuse, define a type predicate whose condition exactly matches its type claim:
#1 Best Overall
function isNonNullish<T>(value: T): value is NonNullable<T> {
return value !== null && value !== undefined;
}
const present = values.filter(isNonNullish);
What filter(Boolean) actually removes
Array.prototype.filter calls its callback for each element and keeps the element when the callback result is truthy. Passing Boolean directly converts each element to a boolean; it is a truthiness test, not a nullish test. JavaScript falsy values include:
false0and-00nNaN""(the empty string)nullundefined
Thus, a truthiness filter is appropriate only when every falsy value should be discarded. The MDN Boolean reference documents the conversion behavior and falsy values.
Rank #2
- 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
Why the distinction matters for TypeScript arrays
Consider counts where zero is a meaningful value:
const counts: Array<number | null | undefined> = [0, 1, null, undefined];
const truthy = counts.filter(Boolean); // runtime result: [1]
const nonNullish = counts.filter(value => value != null); // [0, 1]
The first filter silently loses the valid count 0. The same problem arises if an array’s domain treats false or "" as meaningful. Choose the predicate according to what the data means, rather than using truthiness as shorthand for “present.”
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsFor object-only arrays whose optional members are null or undefined, truthiness can be an effective runtime filter: ordinary objects are truthy. But an explicit nullish check states the intended exclusion more precisely and remains correct if the element type later includes meaningful primitive values.
What TypeScript infers depends on the check and version
Runtime filtering and static type narrowing are related but distinct. JavaScript decides which elements survive based on the callback’s result. TypeScript analyzes callback forms under its type-predicate inference rules; a boolean-returning callback is not automatically a type guard in every case.
TypeScript 5.5 introduced inferred type predicates for suitable functions, and its release notes demonstrate filtering an optional value with .filter(score => score !== undefined). They also explain the scope of truthiness inference: “A truthiness check will infer a type predicate for object types, where there’s no ambiguity.” That object-specific qualification does not make a truthiness check a general-purpose nullish filter for primitive unions. See the TypeScript 5.5 release notes for the version-specific behavior.
For broader guidance on truthiness narrowing and explicit null checks, consult the official TypeScript Handbook section on narrowing.
Quick Recap
Best Value
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.




