概述
undefined:未定义
null:空值,空引用
undefined==null打印出来的是什么?类型又是什么?
console.log(undefined == null) // true
console.log(typeof null) // object
console.log(typeof undefined) // undefined
这两个值都表示“无”的意思
通常情况下, 当我们试图访问某个不存在的或者没有赋值的变量时,就会得到一个 undefined 值。Javascript 会自动将声明是没有进行初始化的变量设为 undifined。
而 null 值表示空,null 不能通过 Javascript 来自动赋值,也就是说必须要我们自己手动来给某个变量赋值为 null。
undefined 和 null 的区别在哪?
- null 是一个表示"无"的对象(空对象指针),转为数值时为 0
- 作为对象原型链的终点出现
- 作为函数参数,表示改函数的参数不是对象
- undefined 是一个表示"无"的原始值,转为数值时为 NaN(not a number)
- 变量被声明了,但没有赋值时,就等于 undefined
- 调用函数时,应该提供的参数没有提供,该参数等于 undefined
- 对象没有赋值的属性,该属性的值为 undefined
- 函数没有返回值时,默认返回 undefined
以上就是我对它们之间的理解,如有不妥,请赐教~