How does the filter(Boolean) in this code work?

function destroyer(arr) {
  var args =;

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < args.length; j++) {
      if (arr[i] === args[j]) {
        delete arr[i];
  return arr.filter(Boolean);

Also what falsey values is it removing? I dont see what could have ended up being falsey. Also what if there was a 0 it was supposed to return but didnt?

Also I thought filter needed a callback function… how does this Boolean work in filter?



It’s the same as doing something like

arr.filter((item) => Boolean(item))

It casts each element of the array as a Boolean for the filtering function.

Ok thanks, howcome you can just write it as


Also why would you want to get rid of the zero’s?

For the same reason that this works:

It depends on the requirements of the function. Zeros are falsy, so if the goal is to remove falsy values, you would remove it.


Is there a way to remove every falsey except 0??

Sure. There isn’t a function that already does exactly that, but you could write a filter function that checks if a value is truthy or zero.

1 Like

Like this?

arr.filter(item => Boolean(item) === 'true' || 0)

What happens when you try it?


It won’t work for 2 reasons.

Because it should be true instead of ‘true’ and maybe I need parantheses?

That’s one problem. Also, you can be more concise because Boolean(item) === true is exactly the same as Boolean(item).

1 Like