@@ -2854,4 +2854,24 @@ function sum(...a){
28542854}
28552855console.log('Sum :', sum(1,2,3)(4,5)(6,7,8,9)()) //"Sum :", 36
28562856=====================================================================================================================================================================
2857+ How to Remove All Falsy Values from an Array in JavaScript:
2858+ *************Using Filter Auto Coercion*************
2859+
2860+ [1, 2, 'b', 0, {}, '', NaN, 3, undefined, null, 5].filter(a => a);
2861+
2862+ *************Using !!*************
2863+
2864+ [1, 2, 'b', 0, {}, '', NaN, 3, undefined, null, 5].filter(a => !!a);
2865+
2866+ *************Using Boolean*************
2867+
2868+ [1, 2, 'b', 0, {}, '', NaN, 3, undefined, null, 5].filter(a => Boolean(a));
2869+
2870+ *************Using Boolean directly*************
2871+
2872+ [1, 2, 'b', 0, {}, '', NaN, 3, undefined, null, 5].filter(Boolean);
2873+
2874+ //This works because Boolean itself is a function, and the arguments filter supplies are passed directly to it.
2875+ //Boolean() is also a function that returns truthy when true and falsy when false!
2876+ ========================================================================================================================================================================
28572877
0 commit comments