# Falsy Bouncer --confused by filter and boolean

Falsy Bouncer --confused by filter and boolean
0

#1

I am utterly confused by the filter syntax and what the purpose of boolean is? I’ve read the provided links and tried to find other sources, but no luck. I am a beginner, so any insight is helpful!

#2

`.filter` allows you to get the elements of an array that satisfy a particular condition. Suppose you have an array of numbers from 1 to 10, and you only want the numbers that are greater than 4. You can use `.filter` to do that:

``````var nums = [1,2,3,4,5,6,7,8,9,10];
var greaterThan4 = nums.filter(function(num) {
return num > 4;
});
console.log(greaterThan4); // [5,6,7,8,9,10]
``````

You’ll provide a function with at least one parameter (`num`) that should return `true` for the elements that you want to keep. Here, because we want to keep all numbers that are greater than 4, we provide a function that checks if a value is greater than 4.

Basically, if the function you provide to `.filter` returns `true` for a particular element in the array, that element is retained.

In JavaScript, it turns out, there are only a handful of values that become `false` when coerced to boolean (the so-called falsy values). Everything else coerce to `true` (the truthy values). You can use the `Boolean` object to coerce any value to `true` or `false`.

``````var x = Boolean(3);
var y = Boolean('hello');
var z = Boolean(null);
console.log(x); // true
console.log(y); // true
console.log(z): // false (because `null` is one of the falsy values)
``````

Now try and combine these two to make a function that removes all falsy values from an array.

#3

Does it matter what the parameter in the function is? I’m confused because “num” is never defined so I don’t understand how you come up with what to put in the function and how that part works. Thank you!

#4

That parameter represents any element in the array, and you can name it anything you want. In the example above I chose to name it `num` because that parameter represents a number. It can be any valid name, really.