Today I Learned - Rocky Kev

TIL checking falsy values inside an array

POSTED ON:

TAGS:

// has falsy values
const theArray = [false, null, 0, 'wizard', undefined, 'barbarian'];

// return true/false if a truthy value exists
const ifAnyIsTrue = theArray.some(Boolean); // true

// return true/false if ALL truthy value exists
const ifAllAreTrue = theArray.every(Boolean); // false

// returns the true values
const giveMeTrueValues = theArray.filter(Boolean);

Related TILs

Tagged:

TIL Sparse Arrays

The ['Batman', , 'Bane'] array is a sparse array. See how it there is a blank?

TIL checking falsy values inside an array

Using the key Boolean to find any true/false statements inside an array

TIL about flattening a JS array