
正文
[jstips]undefined和null的区别
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
undefined是指一个变量没有被声明,或者被声明了但是还没有被赋值null是一个特定值(an assignment value ),代表“没有值”(no value)JavaScript会将没有被赋值的变量默认设为
undefinedJavaScript绝对不自动将一个变量设置为
null值,也就是说null都是程序员手动设置用来说明一个变量没有值的undefined的类型(typeof)是undefinednull的类型(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





