JavaScript Fact #1 How to filter falsy value in Array?

 



As you know most of the guys about JS here are some tips for the filter the falsy value in the array

There are 7 falsy values in JavaScript – false, zero (0), BigInt (0n), empty string ("", '', ``), null, undefined, and NaN. An array in JavaScript permits all types of falsy values. There are several methods to filter falsy values from an array, which are covered below:

Demo:-

1
2
// falsy value here
0, undefined, null'', NaN, false

Example:-

1
2
3
4
5
// define an array
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, 3, NaN ];
// filter the arr
let result=arr.filter(Boolean);
// print result under the console
console.log(result);

Output:-

1
2
/*
OutPut:- [1, 2, 3] */

Post a Comment

0 Comments