定义
全局属性undefined表示原始值undefined。它是一个JavaScript的 原始数据类型 。
应用
- 一个没有被赋值的变量的类型是undefined
// 变量未赋值
let a;
console.log(typeof a) // undefined
// 变量未赋值,输出undefined
let b;
console.log(b) // undefined
- 方法内引用未赋值的变量的类型是undefined
function foo(a){
console.log(typeof a) // undefined
console.log(a) // undefined
}
foo()
3. 方法没有使用return语句指定返回值,默认返回undefined
function foo(a){
console.log(typeof a) // number
console.log(a) // 1
}
foo(1) // undefined
4.方法传入的参数未定义或未赋值,类型也是undefined
function foo(a){
console.log(typeof a) // number
console.log(a) // 1
}
foo(1) // undefined
4. console.log访问未定义的变量, 抛出错误ReferenceError
console.log(c) // Uncaught ReferenceError
console.log(typeof c) //undefined
[1]: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/undefined