undefined是说一个变量没有被声明,或者被声明了但是还没有被赋值null是一个特定值(an assignment value ),代表“没有值”(no value)JavaScript会将没有被赋值的变量默认设为
undefinedJavaScript绝对不自动将一个变量设置为
null值,也就是说null都是程序员手动设置用来说明一个变量没有值的undefined的类型(typeof)是undefinednull的类型(typeof)是object- 两个都是基本数据类型
判断一个变量是
undefined的方法:1typeof(variable) === 'undefined'判断一个变量是
null的方法:1variable === null相等性:(The equility operator consider them equal, but the identity doesn’t):
12null == undefined // truenull === undefined //false
本文是js tips系列,翻译自 https://github.com/loverajoel/jstips