Why does console.log("hello") inside console return undefined?
出于好奇,为什么在控制台中写入 console.log("hello") 会返回 undefined?
在 C 中定义 void 函数是否出于同样的原因?
- 因为 console.log 返回 undefined。
-
所有的 JS 函数都有一个返回值。如果没有明确定义,它会自动返回 undefined。因此,要么 console.log() 显式返回该值,要么不提供显式返回值并将其作为默认值。
console.log 只是写一个文本并返回 undefined。
如果你创建一个函数,添加一个返回值,不会有undefined,它会随着添加的值返回。
示例:
1 2 3 4
| function writer(){
console.log("write new line");
return"ok";
} |
如果在"写入新行"之后调用 writer() 表示在新行中输出"ok"。