JavaScript中哪些情况下会返回undefined ?

146 阅读1分钟

1. 声明了变量 , 但是没有初始化变量

let abc;
console.log(abc)  // undefined

2. 访问一个不存在的属性

let abc = {};
console.log(abc.a)  //undefined

3.访问函数的参数 , 但是参数没有值

(function(a){
    console.log(a)  
})()  //undefined

4.访问的值本身就是 undefined

let abc = undefined
console.log(abc)  // undefined

5. 没有设置return的函数 , 默认返回 undefined

function abc(){}
console.log( abc() )  // undefined

6. 函数设置了return , 但是定义要返回的内容

function abc(){
   return ;
}
console.log( abc() )  // undefined