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.
Practical example
In this example, we use Boolean constructor to remove all falsy values from array.
// ONLINE-RUNNER:browser;
var falsyValues = [false, 0, -0, 0n, '', '', ``, null, undefined, NaN];
var result = falsyValues.filter(Boolean);
console.log(JSON.stringify(result)); // []
Note:
The
resultis an empty array because the input array (falsyValues) contains only values considered as falsy in JavaScript.