Hi, I'm Jacob. Enjoying devFlipCards? Buy me a coffee

7. What's the easies way of removing falsy values from a list?

One of the easiest and cleanest solutions is to filter out falsy values with Boolean constructor, which converts argument to true/false value. You can also use double negation to convert it.

const myArray = ["a", "", [], null, undefined, NaN, 1, {}, 0, true, false]; const onlyTruthy1 = myArray.filter(Boolean); const onlyTruthy2 = myArray.filter((v) => !!v); console.info(onlyTruthy1); // ["a", [], 1, {}, true] console.info(onlyTruthy2); // ["a", [], 1, {}, true]
Struggling to find common date to meet with your friends? Try our new tool
commondate.xyz