Why is null an object and what's the differenc

2018-12-31 05:20发布

Why is null considered an object in JavaScript?

Is checking

if ( object == null )
      Do something

the same as

if ( !object )
      Do something

?

And also:

What is the difference between null and undefined?

20条回答
春风洒进眼中
2楼-- · 2018-12-31 06:10

null is an object. Its type is null. undefined is not an object; its type is undefined.

查看更多
步步皆殇っ
3楼-- · 2018-12-31 06:10

The other fun thing about null, compared to undefined, is that it can be incremented.

x = undefined
x++
y = null
y++
console.log(x) // NaN
console.log(y) // 0

This is useful for setting default numerical values for counters. How many times have you set a variable to -1 in its declaration?

查看更多
登录 后发表回答