null:空值,转为数值为0,类型为object,设值为空值是合理的,例如 var a = null
还有一种情况是未声明,不存在的对象,返回值为null,例如dom中未有id为a的元素,此时document.ElementById('a')返回值为null
undefined:未定义,转为数值为'NaN',类型为undefined
已经声明了,但未赋值的变量,返回undefined,例如var a; 此时,a返回值为undefined
应该注意:
null == undefined 为true
null === undefined 为false
要想准确的获得为null的值,可以如下判断:
var exp = null;
if (!exp && typeof(exp)!="undefined" && exp!=0)
{
alert("is null");
}
if (!exp && typeof(exp)!="undefined" && exp!=0)
{
alert("is null");
}
本文详细解释了JavaScript中null与undefined的区别与联系,包括它们的数值转换、类型判定及如何通过条件语句准确判断一个变量是否为null。
120

被折叠的 条评论
为什么被折叠?



