Sometimes, we want to remove empty strings from array while keeping record without a loop with JavaScript.
In this article, we’ll look at how to remove empty strings from array while keeping record without a loop with JavaScript.
How to remove empty strings from array while keeping record without a loop with JavaScript?
To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter
method.
For instance, we write
const arr = ["I", "am", "", "still", "here", "", "man"];
const filteredArr = arr.filter(Boolean);
to call arr.filter
with Boolean
to return an array with the falsy values filtered out.
Since empty strings are falsy, they won’t be included in the returned array.
Conclusion
To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter
method.