[英]Where is "undefined" coming from here?
I have the following code:我有以下代码:
console.log(doSomething);
doSomething();
function doSomething() {
console.log("Declare something");
}
which in the console returns在控制台中返回
[Function: doSomething]
Declare something
But when I write但是当我写
console.log(doSomething());
function doSomething() {
console.log("Declare something");
}
the console says控制台说
Declare something
undefined
I understand why it just says "Declare something" and not "[Function: doSomething]", but why does it say "undefined"?我明白为什么它只说“声明某事”而不是“[功能:做某事]”,但为什么它说“未定义”? Why does it not say "Declare something Declare something"?为什么它不说“声明某事声明某事”?
Undefined is the return value of the function. Change your function for this for example to prove it. Undefined 是 function 的返回值。为此例如更改您的 function 以证明这一点。
function doSomething() {
console.log("Declare something");
return "test";
}
The second log is regarding your console.log(doSomething());
第二个日志是关于你的console.log(doSomething());
which is printing the return
of your function. In this case, you are not returning anything and therefore it prints undefined
.这是打印你的 function 的return
。在这种情况下,你没有返回任何东西,因此它打印undefined
。
Thats because you didn't declare the return statement of the function.那是因为你没有声明function的return语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.