当前位置: 移动技术网 > IT编程>开发语言>JavaScript > null和undefined的异同

null和undefined的异同

2019年10月16日  | 移动技术网IT编程  | 我要评论

不知道你们有没有发现,null和undefined竟然有这样的关系:

null == undefined     //true
null === undefined  //false

那就来看看他们的异同吧。

相同点:

  • null和undefined在if语句中,都是false。
  • 大体上两者都是代表无

不同点:

  • typeof null === "object" , typeof undefined === "undefined"   这就是为什么 null !== undefined
  • undefined是代表调用一个值而该值却没有赋值,这时候默认则为undefined
  • null是一个很特殊的对象,最为常见的一个用法就是作为参数传入(说明该参数不是对象)
  • 设置为null的变量或者对象会被内存收集器回收
  • null转为数字类型值为0,而undefined转为数字类型为 nan(not a number)

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网