Home >Web Front-end >JS Tutorial >Why Does `console.log` Append 'undefined' in Chrome and Firefox Consoles?
Chrome/Firefox Console.log Output Appends Unexpected "undefined" Line
When using console.log in both Firefox and Chrome on Windows and Linux, an unexplained line stating "undefined" is appended to the output log. This occurrence seems to be consistent across platforms, regardless of the logging function's source (JS file or console).
Explanation
If console.log is invoked from within a JS file, the "undefined" line should not appear. However, when console.log is executed directly from the console itself, it makes sense that "undefined" is printed. Here's why:
The console allows users to type variable names, such as "window," and receive information about them. When a void function (e.g., console.log) is run from the console, it also displays the return value. In this case, since console.log does not return anything, the return value is undefined.
To illustrate, testing in Chrome (Mac ver 23.0.1271.101) showed that "undefined" appears when running console.log inside the console. Additionally, writing the line "var bla = "sdfdfs"" in the console also results in the "undefined" output.
The above is the detailed content of Why Does `console.log` Append 'undefined' in Chrome and Firefox Consoles?. For more information, please follow other related articles on the PHP Chinese website!