NaN属性,isNaN函数

时间:2023-03-09 04:49:34
NaN属性,isNaN函数

NaN:Not a Number,顾名思义,表示不是一个数字。

可以把 Number 对象设置为该值,来指示其不是数字值。

使用 isNaN() 全局函数来判断一个值是否是 NaN 值

详见 JavaScript NaN属性介绍

isNaN函数

通常用于检测 parseFloat() 和 parseInt() 的结果,以判断它们表示的是否是合法的数字。当然也可以用 isNaN() 函数来检测算数错误,比如用 0 作除数的情况。

详见JavaScript isNaN() 函数

实例:

<!DOCTYPE html>
<html>
<head>
<title>测试</title> <meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head>
<script>
function NaNTest()
{
var month = 30;
if(month < 1 || month > 12)
{
month = Number.NaN;
} return month;
}
</script>
<body>
This is my HTML page. 您好 <br> <script>
var month = 30;
if(month < 1 || month > 12)
{
month = Number.NaN;
} document.write("NaNTest:" + month); //输出NaN
</script><br> <script>
document.write("Function_NanTest: &nbsp;" + NanTest().toString()"); //输出NaN
document.write("Function_NanTest: &nbsp;" + isNaN(NaNTest())); // 输出true
</script> <br>

</body>
</html>