Boolean as a function

Consider the following function:

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

The filter method is gonna iterate through the array and apply the function inside it (i.e. Boolean ) to each element of the array and if the element evaluates to true it will include it in the array, my question is, why is Boolean doesn’t have an argument like Boolean(x) ?
for example:

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

Boolean is a function, it can be called as a constructor new Boolean() or just as a function Boolean().

filter takes a callback and in this case, it’s the Boolean function. The callback gets passed each element.

[1,2,3].forEach(callback)

function callback(item) {
  console.log(item); // 1, 2, 3
}
1 Like

MDN has some really cool examples about this:

1 Like