NaN是JavaScript属性,它是非数字值。这表明它不是合法号码。这是语法-
Number.NaN
若要查找数字是否为NaN,请使用Number.isNaN()或isNan()
方法。这是检查的示例-
<!DOCTYPE html> <html> <body> <button onclick="display()">Check</button> <p id="test"></p> <script> function display() { var a = ""; a = a + isNaN(434) + ": 434<br>"; a = a + isNaN(-23.1) + ": -23.1<br>"; a = a + isNaN('Hello') + ": 'Hello'<br>"; a = a + isNaN(NaN) + ": NaN<br>"; a = a + isNaN('') + ": ''<br>"; a = a + isNaN(0) + ": 0<br>"; a = a + isNaN(false) + ": false<br>"; document.getElementById("test").innerHTML = a; } </script> </body> </html>