How to remove falsy values (false, 0, "", null, NaN, undefined) from an array in JavaScript

1 Answer

0 votes
let arr = ["javascript", "", NaN, 0, 5, true, "php", undefined, "c++", false];
 
arr = arr.filter(Boolean);
 
console.log(arr); 
 
   
     
     
/*
run:
     
["javascript", 5, true, "php", "c++"]
     
*/

 



answered Feb 15, 2021 by avibootz
edited May 8, 2022 by avibootz
...