In today’s tutorial, we will hand you the three (3) effective solutions to remove empty strings from an array in JavaScript.
So bear with us as we explore different methods that help you to remove the empty string.
Are you ready? Well, let’s get started!
How to remove an empty string from an array in JavaScript?
To remove empty strings from an array in JavaScript, various approaches can be used.
Here are a few methods you might consider:
Solution 1: Use the filter() method and a truthy check
You can remove an empty string from an array using the filter() method and a truthy check.
Here’s an example:
const SampleArray = ['Programming', '', 'Software Engineering', '', 'Web Development'];
const result = SampleArray.filter(Boolean); ✅
console.log(result);As you can see, we use the filter() function with the Boolean function as the filtering criteria.
Since an empty string is considered a falsy value, it will be removed from the resulting array.
Output:
[ 'Programming', 'Software Engineering', 'Web Development' ]Solution 2: Iterating over the array using a for loop
This method iterates over the array using a for loop and checks each element for an empty string. Only non-empty strings are added to the filteredArray.
Here’s an example:
const SampleArray = ['English', '', 'Math', '', 'Science'];
const filteredArray = [];
for (let i = 0; i < SampleArray.length; i++) { ✅
if (SampleArray[i] !== '') {
filteredArray.push(SampleArray[i]);
}
}
console.log(filteredArray);Output:
[ 'English', 'Math', 'Science' ]Solution 3: Use the reduce() method
You can also use the reduce() method to remove an empty string from an array in JavaScript.
Here’s an example:
const SampleArray = ['Software Engineering', '', 'Programming', '', 'Math'];
const filteredArray = SampleArray.reduce((acc, current) => {✅
if (current !== '') {
acc.push(current);
}
return acc;
}, []);
console.log(filteredArray);This method uses the reduce() function to build the filteredArray. It checks each element for an empty string and accumulates the non-empty strings in the acc parameter.
Output:
[ 'Software Engineering', 'Programming', 'Math' ]Two More Modern Methods (Shorter and Cleaner)
Method 4: filter(Boolean), The One-Liner
The cleanest method in modern JavaScript leverages Boolean as the filter callback. Boolean is a built-in function that returns true for truthy values and false for falsy ones, exactly what we need:
const arr = ["apple", "", "banana", "", "cherry"];
const cleaned = arr.filter(Boolean);
console.log(cleaned);
// Output: ["apple", "banana", "cherry"]Warning:filter(Boolean) removes ALL falsy values: empty strings, null, undefined, 0, false, NaN. If your array contains legitimate 0 or false values, use a stricter check (see Method 5 below).
Method 5: filter with Explicit Type Check (Safest)
const arr = ["apple", "", 0, "banana", false, "cherry"];
const cleaned = arr.filter(item => item !== "");
console.log(cleaned);
// Output: ["apple", 0, "banana", false, "cherry"]
// — preserves 0 and false; removes only empty stringsThis is the right choice when your array mixes empty strings with other falsy values you want to keep. The explicit !== "" comparison removes only the empty string, leaving everything else intact.
Falsy Value Gotchas You’ll Hit
JavaScript has 7 falsy values: "", 0, -0, 0n (BigInt zero), null, undefined, NaN, false. Different “remove empty” methods treat them differently:
| Method | Removes “” | Removes 0 | Removes null/undefined |
|---|---|---|---|
filter(Boolean) | ✓ | ✓ (also) | ✓ (also) |
filter(s => s) | ✓ | ✓ (also) | ✓ (also) |
filter(s => s !== "") | ✓ | ||
filter(s => s.length > 0) | ✓ | crashes on number | crashes on null |
Decision rule:Use filter(Boolean) when you want to drop ALL “empty-ish” values. Use filter(s => s !== "") when you specifically want to keep 0, false, or null.
Handling Whitespace-Only Strings
Strings like " " (a single space) or "\t\n" (tab and newline) are NOT empty, they’re truthy. To treat them as empty:
const arr = ["apple", " ", "banana", "\t", "cherry", ""];
const cleaned = arr.filter(s => s.trim() !== "");
console.log(cleaned);
// Output: ["apple", "banana", "cherry"]This is the right choice for user-input arrays where users might accidentally enter whitespace.
Performance: Which Method Is Fastest?
For everyday arrays (under 10,000 items), all methods perform identically. For large arrays:
- Fastest:
filter(Boolean), function reference avoids per-iteration arrow function creation. - Slightly slower:
filter(s => s !== ""), creates a new arrow function but is still very fast. - Slowest in this list:
reduce(), adds explicit array building, ~15-25% slower for large inputs. - Don’t use:Manual
forloop withsplice(), O(n²) because every splice shifts elements.
Conclusion
This article explores the three (3) effective solutions for removing empty strings from an array in JavaScript.
We’ve illustrated how the filter() method can be used with a truthy check or an explicit check for empty strings.
We’ve also looked at how a traditional for loop can be used to iterate over the array and push non-empty strings to a new array.
Each of these methods has its own advantages and use cases, and they all provide a way to clean up your arrays in JavaScript.
Remember that all these methods return a new array and do not modify the original array. If you want to update the original array, you can assign the result of these methods back to the original array.
Frequently Asked Questions
What is the shortest way to remove empty strings from an array in JavaScript?
The shortest method is arr.filter(Boolean). It uses JavaScript’s built-in Boolean function as the filter predicate, removing every falsy value (empty strings, null, undefined, 0, false, NaN). For one-line readability and brevity, this is the best choice, but be aware it also removes legitimate 0 and false values if your array contains them.
Why does filter(Boolean) remove 0 and false?
Because JavaScript treats 0, false, null, undefined, NaN, and empty strings as “falsy”, all return false when passed to Boolean(). If you want to remove ONLY empty strings while keeping legitimate 0 or false values, use the explicit check: arr.filter(item => item !== "").
How do I remove both empty strings and whitespace-only strings?
Use arr.filter(s => s.trim() !== ""). The trim() method removes leading and trailing whitespace, so a string like ” ” or “\t\n” becomes “”, which then fails the!== “” check and gets filtered out. This is the right approach for user-input arrays.
Does filter() modify the original array in JavaScript?
No, filter() returns a NEW array. The original array is unchanged. If you need to “update” the original variable, reassign: arr = arr.filter(Boolean). This is different from methods like splice() or sort() which mutate the array in place.
Is filter() or reduce() faster for removing empty strings?
Filter() is faster and clearer for this specific task. Reduce() is more flexible (you can transform values during filtering) but adds overhead. For the simple case of removing empty strings, filter(Boolean) or filter(s => s!== “”) is roughly 15-25% faster than the equivalent reduce() on large arrays.
Related JavaScript Tutorials
- How to Remove Commas from a JavaScript String
- JavaScript Date Add Days Methods
- JavaScript Backend Frameworks (2026)
- JavaScript Tutorial Series (Full Index)
- Free JavaScript Projects with Source Code
We hope this article has provided you with enough information to understand the Javascript remove empty string from array.
Thank you for reading Itsourcecoders 😊.
