Why is null an object and what's the difference between null and undefined?

后端 未结 22 1017
猫巷女王i
猫巷女王i 2020-11-22 02:58

Why is null considered an object in JavaScript?

Is checking

if ( object == null )
      Do something

the

22条回答
  •  隐瞒了意图╮
    2020-11-22 03:55

    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?

提交回复
热议问题