Why does Array.filter(Number) filter zero out in JavaScript?

前端 未结 9 1991
时光说笑
时光说笑 2020-12-10 00:10

I\'m trying to filter all non-numeric elements out from an array. We can see the desired output when using typeof. But with Number, it filters zero out.

Here\'s the

9条回答
  •  情书的邮戳
    2020-12-10 01:00

    It's because 0 is a falsy value which returns false, and anything that returns false to the filter function is filtered out of the new array.

    Documentation

    https://developer.mozilla.org/en-US/docs/Glossary/Falsy

提交回复
热议问题