Check for NaN, null and >=0 in one condition

后端 未结 7 1828
一个人的身影
一个人的身影 2021-02-15 14:32

I have a var a;

Its value can be NaN, null and any +ve/-ve number including 0.

I require a condition which filters out all the values of a such that

7条回答
  •  迷失自我
    2021-02-15 14:40

    This seems to work well:

    if (parseFloat(x) === Math.sqrt(x*x))...
    

    Test:

    isPositive = function(x) { return parseFloat(x) === Math.sqrt(x*x) }
    a = [null, +"xx", -100, 0, 100]
    a.forEach(function(x) { console.log(x, isPositive(x))})
    

提交回复
热议问题