[jstips]undefined和null的区别

  • undefined是指一个变量没有被声明,或者被声明了但是还没有被赋值

  • null是一个特定值(an assignment value ),代表“没有值”(no value)

  • JavaScript会将没有被赋值的变量默认设为undefined

  • JavaScript绝对不自动将一个变量设置为null值,也就是说null都是程序员手动设置用来说明一个变量没有值的

  • undefined的类型(typeof)是undefined

  • null的类型(typeof)是object

  • 两个都是基本数据类型

  • 判断一个变量是undefined的方法:

typeof(variable) === 'undefined'
  • 判断一个变量是null的方法:
variable === null
  • 相等性:(The equility operator consider them equal, but the identity doesn't):
null == undefined // true
null === undefined  //false

本文是js tips系列,翻译自 https://github.com/loverajoel/jstips

原文地址:https://www.cnblogs.com/le0zh/p/undefined_and_null.html