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

前端 未结 9 1988
时光说笑
时光说笑 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:15

    Zero is a falsey value. The typeof is always returning a boolean value. When the number 0 is returned, it is returning to the test, and therefore coming back as false, so the number zero is filtered out.

提交回复
热议问题