Created
January 30, 2017 06:46
-
-
Save mijimoco/9128355d48673f37417b633d2db5a582 to your computer and use it in GitHub Desktop.
Falsy Bouncer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bouncer (arr) { | |
var bouncedArr = arr.filter(Boolean); | |
console.log(bouncedArr); | |
return bouncedArr; | |
} | |
bouncer([7, "ate", "", false, 9]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Boolean is an object wrapper for boolean values. It's not crystal clear to me what the F that means, but it's very useful. Why? Because as the documentation says "If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false", which is great, as that's exactly what we need inside our filter. We want items that do not fulfill our filter, or false items will be filtered out. That's why arr.filter(Boolean); gets rid of all Nan undefined or "" items