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.