关于javascript:为什么控制台内的console.log(“hello”)返回未定义?

Why does console.log("hello") inside console return undefined?

本问题已经有最佳答案,请猛点这里访问。

出于好奇,为什么在控制台中写入 console.log("hello") 会返回 undefined?

在 C 中定义 void 函数是否出于同样的原因?

enter


console.log 只是写一个文本并返回 undefined。
如果你创建一个函数,添加一个返回值,不会有undefined,它会随着添加的值返回。
示例:

1
2
3
4
function writer(){
    console.log("write new line");
    return"ok";
}

如果在"写入新行"之后调用 writer() 表示在新行中输出"ok"。