js 输出undefined的原因
- 变量未定义:如果尝试输出未定义的变量,会得到 undefined。例如,如果这样写:
var x;
console.log(x);
输出结果将是 undefined。
- 函数没有返回值:如果尝试输出一个没有返回值的函数的结果,则会得到 undefined:
function test() {
console.log('Hello, World!');
}
var result = test(); // undefined
- 对象属性不存在:如果尝试访问对象中不存在的属性,则会得到 undefined:
var person = {
name: 'John',
age: 30
};
console.log(person.email); // undefined
- 函数参数没有传递:如果尝试访问未传递的函数参数,则会得到 undefined:
function test(x) {
console.log(x);
}
test(); // undefined
- 函数参数没有默认值:如果尝试访问未传递的函数参数且该参数没有默认值,则会得到 undefined:
function test(x) {
console.log(x);
}
test(undefined); // undefined
``
原文地址: https://www.cveoy.top/t/topic/cgRe 著作权归作者所有。请勿转载和采集!