美文网首页
JS中如何判断是否是undefined和null

JS中如何判断是否是undefined和null

作者: Ggx的代码之旅 | 来源:发表于2016-09-21 11:23 被阅读62次
  • 判断undefined
    我们知道typeof 返回的是字符串,有六种可能:
    "number"、"string"、"boolean"、"object"、"function"、"undefined"
    因此我们可以这么做:
if (typeof(reValue) == "undefined") {
    alert("undefined");
}
  • 判断null
var exp = null;
if (!exp && typeof(exp)!="undefined" && exp!=0){
    alert("is null");
} 

尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。

相关文章

网友评论

      本文标题:JS中如何判断是否是undefined和null

      本文链接:https://www.haomeiwen.com/subject/sohaettx.html