
JavaScript中判断null(空)错误的方法:
var exp = null;
if (exp == null)
{
alert(“is null”);
}
//exp 为 undefined 时,也会得到与 null 相同的结果,虽然 null 和 undefined 不一样。
//注意:要同时判断 null 和 undefined 时可使用本法。
var exp = null;
if (!exp)
{
alert(“is null”);
}
//如果 exp 为 undefined 或者数字零,也会得到与 null 相同的结果,虽然 null 和二者不一样。
//注意:要同时判断 null、undefined 和数字零时可使用本法
var exp = null;
if (typeof(exp) == “null”)
{
alert(“is null”);
}
//为了向下兼容,exp 为 null 时,typeof 总返回 object
var exp = null;
if (isNull(exp))
{
alert(“is null”);
}
//JavaScript 中没有 isNull 这个函数。JavaScript判断null(空)正确的方法:
var exp = null;
if (!exp && typeof(exp)!=”undefined” && exp!=0)
{
alert(“is null”);
} 我们在DOM应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。
null:
值null特指对象的值未设置。它是JavaScript基本类型 之一。
值null是一个字面量,它不像undefined是全局对象的一个属性。null是表示缺少的标识,指示变量未指向任何对象。把null作为尚未创建的对象,也许更好理解。在APIs中,null常在返回类型是对象,但没关联值的地方使用。
以上就是javascript怎么判断null(空)?的详细内容,更多请关注易知道|edz.cc其它相关文章!














