How do you check that a number is NaN in JavaScript?

前端 未结 30 3138
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:19

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:

parseFloat(\'geoff\') == NaN;

parseFloat(\'ge         


        
30条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 06:55

    I've created this little function that works like a charm. Instead of checking for NaN which seems to be counter intuitive, you check for a number. I'm pretty sure I am not the first to do it this way, but I thought i'd share.

    function isNum(val){
        var absVal = Math.abs(val);
        var retval = false;
        if((absVal-absVal) == 0){
            retval = true
        }
    
        return retval;
    }
    

提交回复
热议问题