40
function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    function a(b) {
      if(b !== false) {
        return b;
      }
    }

    arr = arr.filter(a);
    return arr;
}

bouncer([7, 'ate', '', false, 9]);

I have to return true boolean statements only, and when I run this code, it works. However, I am quite confused because my "if statement" will work whether it's b !== true or b !== false. Could someone please explain the reason why this works both ways?

jyoon006
  • 739
  • 3
  • 9
  • 14
  • 1
    Related - [Remove empty elements from an array in Javascript](https://stackoverflow.com/a/2843625/104380) – vsync Jul 21 '18 at 13:45
  • It's amazing how many people in here didn't even answer the actual question you were asking. – BadHorsie May 26 '21 at 09:37

8 Answers8

82

Apparently .filter() was introduced in ES5.

This definitely helped me wrap my mind around what's going on here. Hope it helps!

Essentially, writing:

arr.filter(Boolean)

is the same as writing:

arr.filter( function(x) { return Boolean(x); }); 

since Boolean() is also a function that returns truthy when true and falsy when false!

Example:

var a = [1, 2, "b", 0, {}, "", NaN, 3, undefined, null, 5];

var b = a.filter(Boolean);  // [1, 2, "b", {}, 3, 5]; 

Source: here.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
leonelaguzman
  • 941
  • 6
  • 5
60

I was solving a similar problem and came up with this:

function bouncer(arr) {
  return arr.filter(Boolean);
}
console.log(bouncer([7, 'ate', '', false, 9]));
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
hello world
  • 1,727
  • 6
  • 21
  • 37
15

simplest way to do it :

function bouncer(arr) {
    return arr.filter(x => !!x);
}
mahig
  • 151
  • 1
  • 3
6

It's because you return the value. The filter function should return true or false like this:

function bouncer(arr) {
    arr = arr.filter(function(x) { console.log(x === true)
       if(x !== false) {
           return true;
       }
    });
    return arr;
}

or shorter:

function bouncer(arr) {
    return arr.filter(function(x) { console.log(x === true)
       return x !== false;
    });
}
Emil Ingerslev
  • 4,645
  • 2
  • 24
  • 18
3

Your function acts is it does because JavaScript values in boolean comparisons are "truthy" or "falsey". Non-booleans are coerced to a boolean value when used in a boolean context (comparisons, if statements, etc.)

If I understand your intent, you can modify your function to get your expected output like this:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    function a(b) {
      if(typeof(b) === 'boolean' && !b) {
        return new Boolean(b);
      }
    }

    arr = arr.filter(a);
    return arr;
}

bouncer([7, 'ate', '', false, 9, true]);
jdphenix
  • 15,022
  • 3
  • 41
  • 74
2

I came across this exercise and was playing about with it somewhat. It ended up helping me understand it a bit better, so if you may I will add a bit of clarity to this bouncer function.

function bouncerOne(array){

let truthy = array.filter(x => x);
// This takes the .filter() and applies the anon function where it takes 
// the value and returns the positive value. All values that are not Falsy including 
// objects and functions would return True. 
console.log("The Truth is:", truthy);//this Outputs the result to the console.
let falsy = array.filter(x => !x); 
//This takes the .filer() and applies the 
//annon function where it takes the value and only returns items that ARE NOT True. 
//So Falsy Values are taken. 
console.log("The Falsy Values are",falsy);

//The annon function of (x => x) & (x => !x)
// really makes use of the strict equality power of JavaScript. 
// Where simple conditional test can be carried out.
};
console.log(bouncerOne([7, "ate", "", false, 9,null,42,"Bingo",undefined,NaN]));
//Returns: The Truth is: [ 7, 'ate', 9, 42, 'Bingo' ]
//The Falsy Values are [ '', false, null, undefined, NaN ]
Dr Gaud
  • 97
  • 3
0

Double Not operator ( !! ) convert value into Boolean form. eg : !!true // true;


let a = [1, 2, "b", 0, {}, "", NaN, 3, undefined, null, 5];

// writing in ES6 syntax
a.filter(v=>!!v); // [1, 2, 'b', {…}, 3, 5]

// writing with function keyword
a.filter( function(v) { return (!!v); }); // [1, 2, 'b', {…}, 3, 5]

Zehan Khan
  • 21
  • 3
0

So basically, your if statement is not doing anything in this code because it is not changing arr which you are returning as output. If you set b!== true or false it has no effect. Even if you remove the "if statement", you will get same answer.