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
Use filter() with an explicit comparison to remove only empty strings: const cleaned = values.filter(value => value !== "");. This keeps whitespace-only strings and other values, returns a new array, and leaves the original array unchanged.
Remove only exact empty strings
For an array of strings, compare each value with the empty string:
const values = ["apple", "", "banana", ""];
const cleaned = values.filter((value) => value !== "");
console.log(cleaned); // ["apple", "banana"]
filter() keeps each element for which the callback returns a truthy result. It creates a shallow copy of the selected elements rather than modifying the original array. See MDN’s documentation for Array.prototype.filter().
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Choose what counts as empty
An exact empty string, whitespace-only text, and a falsy value are different cases. Pick the predicate that matches your data rules.
#1 Best Overall
| Requirement | Predicate | What remains |
|---|---|---|
Remove only "" |
value !== "" |
Whitespace-only strings and other values remain. |
| Remove empty and whitespace-only strings | value.trim() !== "" |
Whitespace-only strings are excluded; nonblank strings are retained as originally stored. |
| Remove all falsy values | Boolean(value) |
Falsy values of any type are excluded, not just empty strings. |
Whitespace-only strings
If spaces or other trim-recognized whitespace should count as blank, test the trimmed value:
const values = ["apple", "", " ", " banana "];
const cleaned = values.filter((value) => value.trim() !== "");
// ["apple", " banana "]
The predicate uses trim() to decide what to exclude, but it does not replace retained strings with trimmed versions. If you need to normalize their contents too, map them through trim() separately.
Rank #2
Why not use filter(Boolean)?
filter(Boolean) removes every falsy value. In addition to "", this includes 0, NaN, 0n, null, and undefined. That can silently discard meaningful values from a mixed array. The TypeScript Handbook explains the relevant truthiness and narrowing behavior. Use the Boolean shortcut only when removing all falsy values is actually intended.
Use the same exact check in TypeScript
For an array whose elements are strings, the JavaScript comparison works in TypeScript without a special helper:
const values: string[] = ["apple", "", "banana"];
const cleaned = values.filter((value) => value !== "");
// string[]
When an array is a union of types, compare against the particular value you want to remove. For example, an exact empty-string test preserves both numbers and null:
const mixed: Array<string | number | null> = ["", "apple", 0, null];
const cleaned = mixed.filter((value) => value !== "");
// ["apple", 0, null]
Keep or remove undefined deliberately
If the array may contain undefined, filtering out empty strings does not remove it. Apply a second explicit check if that is also part of the requirement:
Rank #4
const values: Array<string | undefined> = ["apple", "", undefined, "banana"];
const withoutEmptyStrings = values.filter((value) => value !== "");
// (string | undefined)[]; undefined remains
const withoutEmptyStringsOrUndefined = values.filter(
(value) => value !== "" && value !== undefined,
);
// string[]
TypeScript 5.5 can infer type predicates from suitable checks, including checks against undefined. The exact inferred type can depend on the expression and the project’s TypeScript version; see the TypeScript 5.5 release notes. Explicit comparisons keep the removal rule visible and avoid using truthiness where valid values such as zero must survive.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
What happens with sparse arrays?
filter() calls its callback for assigned indexes; holes in a sparse array are skipped and do not become elements in the filtered result. If every assigned element fails the predicate, the result is an empty array. These behaviors are described in MDN’s filter() reference.
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.

