EN
JavaScript - remove all falsy values from array
0 points
In this article, we would like to show you how to remove all falsy values from an array in JavaScript.
In this example, we use Boolean
constructor to remove all falsy values from array.
xxxxxxxxxx
1
var falsyValues = [false, 0, -0, 0n, '', '', ``, null, undefined, NaN];
2
3
var result = falsyValues.filter(Boolean);
4
5
console.log(JSON.stringify(result)); // []
Note:
The
result
is an empty array because the input array (falsyValues
) contains only values considered as falsy in JavaScript.