[javascript|基本概念|Underfined]学习笔记

Underfined类型的值:underfined(只有一个)

1/声明未初始化

e.g.:var msg;-->msg == underfined:true

2/申明并值初始化为underfined

e.g.:var msg = underfined;-->msg == underfined:true

3/未声明

e.g.:msg;-->Error:msg is not defined

只能用typeof检测

判断是否声明或未申明

try{

  if(msg == underfined || msg){

    console.log("exist");

  }

}catch(e){

  console.log("not exist");

  console.log(e);

}

try{

  msg in window;

  console.log("exist");

}catch(e){

  console.log("not exist");

}

建议:一般值为underfined的变量不需要显式赋值,但是只要声明变量,就初始化是定义变量最好的习惯。

原文地址:https://www.cnblogs.com/bsj2016/p/5437923.html