JS中如何判断null、undefined与NaN

时间:2022-09-16 05:19:31

//判断undefined:
var tmp = undefined;
if (typeof(tmp) == "undefined"){
alert("undefined");
}

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

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

//判断undefined和null://
var tmp = undefined;
if (tmp== undefined)
{
alert("null or undefined");
}

var tmp = undefined;
if (tmp== null)
{
alert("null or undefined");
}
//说明:null==undefined

//判断undefined、null与NaN://
var tmp = parseInt("sdfe");
if ((!tmp)&&(tmp!=0))
{
alert("null or undefined or NaN");
}