码迷,mamicode.com
首页 > 其他好文 > 详细

如何区分null和undefined

时间:2018-11-15 13:46:18      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:场景   def   null   应对   efault   使用   使用场景   int   区分   

null和undefined是两种数据类型, 如果硬要区分的话。

null是一种类型, 赋值变量为null型。

未定义的变量, 即为undefined。

var a = null
a  // null
var b
b  // undefined
typeof(b)  // "undefined"

标准解释

6.1.1 The Undefined Type
The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

6.1.2 The Null Type
The Null type has exactly one value, called null.

undefined使用场景:

  • 判断变量, 判断函数传参异常, 判断对象字段和方法是否存在(或者正确使用Object.propretype.call(ob, ‘key‘))
  • es6中应对eslint规范的export default undefined

如何区分null和undefined

标签:场景   def   null   应对   efault   使用   使用场景   int   区分   

原文地址:https://www.cnblogs.com/daaasheng/p/9962668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!