EN
JavaScript - remove empty strings from array without loop
0 points
In this article, we would like to show you how to remove empty strings from an array without using any loop in JavaScript.
In this example, we use filter()
method with passed Boolean
as an argument to check for truthy values. The empty strings (''
, ""
, ``
) are falsy values, so they will be removed.
xxxxxxxxxx
1
var array = ['a', '', 'b', "", 'c', ``];
2
3
array = array.filter(Boolean);
4
5
console.log(array); // [ 'a', 'b', 'c' ]
In this example, we present an alternative solution using the arrow function as filter()
method argument.
xxxxxxxxxx
1
var array = ['a', '', 'b', "", 'c', ``];
2
3
array = array.filter((element) => element);
4
5
console.log(array); // [ 'a', 'b', 'c' ]