markmcdermott.io (js, ruby, ...etc) by mark mcdermott

arr.some(Boolean)

03/26/2024

estimated reading time:1 min

I learned today a quick way to test a bunch of boolean expressions in Javascript is to put them in an array and use Javascript’s some or every method:

arr.some(Boolean) arr.every(Boolean)

.some(Boolean) will return true if any element of the array is truthy. .every(Boolean) will return true if all elements in the array are truthy.

Similarly, arr.filter(Boolean) will remove all falsy values from an array. Falsy values would be 0, undefined, NaN, false, "".

Source: https://www.30secondsofcode.org/js/s/check-array-values-are-truthy/#check-if-all-values-in-an-array-are-truthy