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]
devFlipCards 2024

Do you accept cookies?

Cookies are small amounts of data saved locally on you device, which helps our website - it saves your settings like theme or language. It helps in adjusting ads and in traffic analysis. By using this site, you consent cookies usage.