JS 判断空值

(1)typeof用法

typeof的运算数未定义,返回的就是 "undefined".

运算数为数字 typeof(x) = "number"

字符串 typeof(x) = "string"

布尔值 typeof(x) = "boolean"

对象,数组 和null typeof(x) = "object"

函数 typeof(x) = "function"

(2)js判断是否为空

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

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

转自:http://blog.sina.com.cn/s/blog_755168af0100vsik.html

原文地址:https://www.cnblogs.com/evilrogue/p/3003662.html