While working on my questions for Bridge School, I came across this neat pice of code. Say you have an array, and you want to remove all falsey items and return a new array:
myCoolArray = [3, 2, "", false, NaN, 5, 0, null, undefined] filteredArray = myCoolArray.filter(Boolean) console.log(filteredArray); // [3, 2, 5]
Volia! Magic! I believe this works because Boolean acts as a function that coerces each value in the array to a boolean. It returns true for truthy values and false for falsey values. The filter function then returns all values that are true.