Chrome/Firefox console.log always appends a line saying 'undefined'

前端 未结 7 1656
时光说笑
时光说笑 2020-11-21 13:40

Every time console.log is executed, a line saying undefined is appended to the output log.

It happens in both Firefox and Chrome on Windows

7条回答
  •  自闭症患者
    2020-11-21 14:01

    Although talkol´s answer is ok, I try to put it more straight:

    JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.

    So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.

    [If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]

    Sources:

    • https://stackoverflow.com/a/20915524/1744768
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

提交回复
热议问题