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 errorsUse filter() with an explicit comparison to remove only exact empty strings: value !== "". This keeps whitespace-only strings and other values, unlike filter(Boolean), which removes every falsy value.
Remove exact empty strings with filter()
For an array of strings, pass a predicate that rejects only the empty string:
As an Amazon Associate I earn from qualifying purchases.
const values = ["apple", "", "banana", ""];
const cleaned = values.filter((value) => value !== "");
console.log(cleaned); // ["apple", "banana"]
filter() creates a shallow copy containing the elements whose callback returns a truthy result; it does not modify the original array. See MDN’s documentation for Array.prototype.filter().
Choose what counts as “empty”
| Requirement | Predicate | What remains |
|---|---|---|
| Remove only exact empty strings | value !== "" |
Whitespace-only strings such as " " remain. |
| Remove empty and whitespace-only strings | value.trim() !== "" |
Whitespace-only strings are removed; other strings are kept in their original, untrimmed form. |
| Remove every falsy value | Boolean(value) or filter(Boolean) |
All falsy values are removed, not just empty strings. |
For example, use trimming only if your application treats whitespace-only input as blank:
#1 Best Overall
const values = ["apple", "", " ", "banana"];
const cleaned = values.filter((value) => value.trim() !== "");
console.log(cleaned); // ["apple", "banana"]
This tests a trimmed version of each string but preserves the original value of every string that passes. If you also want to normalize the remaining strings by trimming them, that is a separate transformation.
Why filter(Boolean) can remove too much
filter(Boolean) keeps values that coerce to true and removes values that coerce to false. In addition to "", JavaScript treats values including 0, NaN, 0n, null, and undefined as falsy. That can silently discard meaningful values from a mixed array. The TypeScript Handbook’s narrowing guide explains the implications of truthiness checks, while the TypeScript 5.5 release notes call out the risk of truthiness filters when values such as numeric zero are valid.
Rank #2
const mixed = ["", "hello", 0, null];
const onlyNonEmptyStringsAndOtherValues = mixed.filter((value) => value !== "");
// ["hello", 0, null]
const allTruthyValues = mixed.filter(Boolean);
// ["hello"]
TypeScript: make the removal rule explicit
For a string array, the exact comparison works the same way:
const values: string[] = ["apple", "", "banana"];
const cleaned = values.filter((value) => value !== "");
// string[]
For a union array, the same predicate removes only the empty-string member and preserves other values:
const mixed: Array<string | number | null> = ["", "hello", 0, null];
const cleaned = mixed.filter((value) => value !== "");
// (string | number | null)[]
If the array can also contain undefined, decide separately whether it should remain. These two filters express two different rules:
const values: Array<string | undefined> = ["apple", "", undefined, "banana"];
const withoutEmptyStrings = values.filter((value) => value !== "");
// Keeps undefined
const withoutEmptyStringsOrUndefined = withoutEmptyStrings.filter(
(value) => value !== undefined,
);
// Removes undefined too
TypeScript 5.5 can infer type predicates from suitable checks, such as a comparison against undefined, and narrow the resulting array. The inferred result depends on the predicate and TypeScript version; use a check that states the intended condition rather than relying on broad truthiness when valid values might be falsy.
Rank #4
What happens to the original array and sparse arrays?
The filtered result is a new shallow array, so assigning it to a new variable leaves the source array unchanged. Elements themselves are not deep-copied. For sparse arrays, filter() calls the callback only for assigned indexes; holes are skipped and are not added to the result. If no elements pass the predicate, the result is an empty array. These behaviors are documented by MDN.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteQuick 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.




