欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中判斷null、undefined與NaN的方法

 更新時(shí)間:2014年03月26日 16:20:36   投稿:whsnow  
這篇文章主要介紹了JS中判斷null、undefined與NaN的方法,需要的朋友可以參考下

寫了個(gè) str ="s"++;

然后出現(xiàn)Nan,找了一會(huì)。

收集資料如下判斷:

1.判斷undefined:

var tmp = undefined; 
if (typeof(tmp) == "undefined"){ 
alert("undefined"); 
}

說明:typeof 返回的是字符串,有六種可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"

2.判斷null:

var tmp = null; 
if (!tmp && typeof(tmp)!="undefined" && tmp!=0){ 
alert("null"); 
}

3.判斷NaN:

var tmp = 0/0; 
if(isNaN(tmp)){ 
alert("NaN"); 
}

說明:如果把 NaN 與任何值(包括其自身)相比得到的結(jié)果均是 false,所以要判斷某個(gè)值是否是 NaN,不能使用 == 或 === 運(yùn)算符。

提示:isNaN() 函數(shù)通常用于檢測(cè) parseFloat() 和 parseInt() 的結(jié)果,以判斷它們表示的是否是合法的數(shù)字。當(dāng)然也可以用 isNaN() 函數(shù)來檢測(cè)算數(shù)錯(cuò)誤,比如用 0 作除數(shù)的情況。

4.判斷undefined和null:

var tmp = undefined; 
if (tmp== undefined) 
{ 
alert("null or undefined"); 
}
var tmp = undefined; 
if (tmp== null) 
{ 
alert("null or undefined"); 
}

說明:null==undefined

<!--EndFragment-->

5.判斷undefined、null與NaN:

var tmp = null; 
if (!tmp) 
{ 
alert("null or undefined or NaN"); 
}

提示:一般不那么區(qū)分就使用這個(gè)足夠。

相關(guān)文章

最新評(píng)論