JS中判断null、undefined与NaN的方法

2018-4-21    分类: 前端资源

js 判断 类型

//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"); 
}

 

 

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


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

 

 

 

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

 

http://www.jb51.net/article/48481.htm