I just found this problem when use Array.prototype.filter(), someone can explain to me why?
I have these two arrays
[0, NaN, NaN, NaN]
[1, NaN, NaN, NaN]
When I call filter() using Number type filter the return of first is an empty array with instead the second return is an array.
I think the check on 0 as a Number fail but why? I tried to construct a Number with 0 and it work obviously.
console.log([0, NaN, NaN, NaN].filter(Number)); // []
console.log([1, NaN, NaN, NaN].filter(Number)); // [1]
// other examples
console.log([0, 1, 2, 3].filter(Number)); // [1, 2, 3]
console.log([1, 2, 3, 4].filter(Number)); // [1, 2, 3, 4]
console.log([0.1, 0.2, 0.3, 0.4].filter(Number)); // [0.1, 0.2, 0.3, 0.4]
console.log([1.1, 2.2, 3.3, 4.4].filter(Number)); // [1.1, 2.2, 3.3, 4.4]
console.log([-1, -2, -3, -4].filter(Number)); // [-1, -2, -3, -4]
Thank you in advance guys and girls.
Number(0)returns0which is falsey. Why would you expect that to just pass?Numberdoesn't return boolean values, after all.Numbertype filter" wait, that's not a type filter at all. You should probably re-check the documentation on whatfilterdoes - it expects a predicate, andNumberis not one.Number()function doesn't tell you whether something is a number, it converts it to a number.