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

前端 未结 30 3127
伪装坚强ぢ
伪装坚强ぢ 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:47

    alert("1234567890.".indexOf(String.fromCharCode(mycharacter))>-1);
    

    This is not elegant. but after trying isNAN() I arrived at this solution which is another alternative. In this example I also allowed '.' because I am masking for float. You could also reverse this to make sure no numbers are used.

    ("1234567890".indexOf(String.fromCharCode(mycharacter))==-1)
    

    This is a single character evaluation but you could also loop through a string to check for any numbers.

提交回复
热议问题