3 回答
TA贡献1946条经验 获得超4个赞
alert(typeof(null)); // objectalert(typeof(undefined)); // undefinedalert(null !== undefined) //truealert(null == undefined) //true
object == null
if ( !object )
.
! Boolean(object)
!
Boolean(null)
!false === true
.
alert(Boolean(null)) //falsealert(Boolean(0)) //falsealert(Boolean("")) //false
TA贡献1828条经验 获得超6个赞
null
typeof null
"object"
null
undefined
undefined
*由JavaScript使用,意思是“无值”。未初始化变量、缺少参数和未知变量都有此值。 > var noValueYet;> console.log(noValueYet);undefined> function foo(x) { console.log(x) }> foo()undefined> var obj = {}; > console.log(obj.unknownProperty)undefined
但是,访问未知变量会产生异常: > unknownVariableReferenceError: unknownVariable is not defined
null
*程序员用来表示“无值”,例如作为函数的参数。
console.log(typeof unknownVariable === "undefined"); // truevar foo;console.log(typeof foo === "undefined"); // trueconsole.log(foo === undefined); // truevar bar = null;console.log(bar === null); // true
x == null
null
undefined
:
> null == nulltrue> undefined == nulltrue
true
if
function foo(param) { if (param) { // ... }}function foo(param) { if (! param) param = "abc";}function foo(param) { // || returns first operand that can't be converted to false param = param || "abc";}
false
undefined
0
).
undefined
,null
布尔人: false
数字: +0
,-0
,NaN
字符串: ""
Boolean
new
):
> Boolean(null)false> Boolean("")false> Boolean(3-3)false> Boolean({})true> Boolean([])true
添加回答
举报