undefined
Undefined类型的唯一值就是undefined,使用var和let声明的变量没有初始化,它的值就是undefined,而使用const定义变量时,必须初始化
引入undefined是为了正式区分空对象指针和未初始化的变量
var test;
console.log(typeof test);
console.log(typeof test1);
//可以发现,不管变量是否声明,typeof操作符都会返回undefined,所以用typeof并不能区分变量是否声明与否
try{
// 已经声明
// 判断是否已经定义
if (va === undefined){
window.console && console.log("变量未定义.");
}else {
// 已经定义了,可能为null
window.console && console.log("变量已定义.");
}
} catch(e){
// va 未声明
window.console && console.log("变量未声明,");
}
//这个try catch语句可以区分变量是否声明,是否定义(百度来的),如有更好的方法烦请留言科普