Tell us what’s happening:
This is my code for Falsy Bouncer:
function bouncer(arr) {
// Don’t show a false ID to this bouncer.
var newArr = arr.filter(function(a) {
return (a !== NaN && a !== undefined && a !== “” && a !== 0 && a != null && a !== false);
});
return newArr;
}
bouncer([1, null, NaN, 2, undefined]);
It works for everything except null, can anyone explain why? Thank you
Your code so far
function bouncer(arr) {
// Don't show a false ID to this bouncer.
var newArr = arr.filter(function(a) {
return (a !== NaN && a !== undefined && a !== "" && a !== 0 && a != null && a !== false);
});
return newArr;
}
bouncer([1, null, NaN, 2, undefined]);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/falsy-bouncer
This is a common misunderstanding with the Falsy Bouncer. You can make a solution for this challenge which tests against a hardcoded list of falsy values, but it misses the point of the exercise. The concept of values being “falsy” isn’t something made up by Free Code Camp for the purposes of this challenge, but rather a quality that values have in programming languages. A value is falsy if the language treats the value as false
when it is evaluated as a boolean. This means that Boolean(someFalsyValue)
is false
but also that we can use it in a conditional like if (someFalsyValue)
. We can also talk about values being “truthy”. As I’m sure you’ve guessed, truthy values evaluate to true
in a boolean of logical context.
if (someFalsyValue) {
// this code block is not entered
}
else if (someTruthyValue) {
// this code block is entered and executed
}
Thank you. I got it right with this code:
function bouncer(arr) {
var newArr = arr.filter(function(a) {
return a;
});
return newArr;
}
Just to be certain I understand this correctly. The function filtered removed all the falsy values because the programming treats them as incorrect values that need to be removed from the array?
I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.

The way that the filter function works is that it evaluates the returned value as a boolean. Because the definition of "falsy" is that they are treated like `false` when evaluated as a boolean it means that all our falsy values are removed by the filter function.
Neat, huh?
Truthiness/falsiness is a really useful tool.
1 Like