undefined

2018-02-18大约2分钟

undefined是一个全局的熟悉,表示一个值是未定义的。

用法说明

一个没有被赋值的变量的类型是undefined

let a;
console.log(a);

如果有其他语言基础的同学,需要注意这一点和其他语言是不一样的:其他语言没有undefined值,未赋值的变量的值都是null

访问未定义的变量,程序会报异常:

console.log(a);

要避免错误,一定要先用typeof判断:

if (typeof a === "undefined") {
    console.log("a is undefined");
}