Sometimes, we want to remove empty strings from array while keeping record without loops with JavaScript.
In this article, we’ll look at how to remove empty strings from array while keeping record without loops with JavaScript.
How to remove empty strings from array while keeping record without loops with JavaScript?
To remove empty strings from array while keeping record without loops with JavaScript, we call the filter
method.
For instance, we write
const arr = ["I", "am", "", "still", "here", "", "man"];
const newArr = arr.filter(Boolean);
to call arr.filter
with Boolean
to return a new array without the falsy values in arr
.
Empty string is a falsy value so it’s filtered out.
Conclusion
To remove empty strings from array while keeping record without loops with JavaScript, we call the filter
method.