Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Remove Empty Strings from an Array in JavaScript and TypeScript

Remove exact empty strings with `filter(value => value !== "")`. Keep whitespace or mixed values intentionally, and avoid `filter(Boolean)` when falsy values such as 0 must survive.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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().

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

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:

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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

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.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.