Undefined
未定义的值 分为以下4种情况
1、声明一个变量,但是没有赋值
var fo;
console.log(fo);//undefined
访问fo,返回undefined,变量声明后,未使用,也没有定义过任何有效值
2、访问对象上不存在的属性或者未定义的变量
console.log(Object.fn);//undefined
console.log(typeof foo);//undefined
访问Object对象上的fn属性,返回undefined,表示Object上不存在或者没有定义名为fn的属性;
对未声明变量执行typeof操作符返回了undefined
3、函数定义了形参,但没有传递实参
function fn(a){
console.log(a);//undefined
}
fn();
函数fn定义了形参a,但fn被调用时没有传递参数,因此,fn运行时的参数a就是一个原始的、未被赋值的变量
4、使用void表达式求值
console.log(void null);//undefined
console.log(0);//undefined
console.log(false);//undefined
console.log(null);//undefined
console.log([]);//undefined
void操作符对任何表达式求值都返回 undefined
null
空值,表示一个对象被人为的重置为空对象,而非一个变量最原始的状态
- 如果定义的变量将来用于保存对象,该变量初始化为null,而不是其他值
- 当一个数据不再需要使用时,通过将其值设置为null来释放其引用
总结
undefined表示一个变量自然、最原始的状态值 null则表示一个变量被人为设置为空对象,而不是原始状态,当需要释放一个对象时,直接赋值为null