Undefined Output After Console.log() in Chrome and Firefox
Problem:
When executing console.log() in Firefox or Chrome, an additional line saying "undefined" appears in the output log, regardless of the operating system.
Answer:
The "undefined" output is not appended if console.log() is executed from a JavaScript file. However, if it is called directly from the console, the "undefined" line is expected.
Explanation:
When console.log() is used from the console, it behaves differently than when executed from a script. In the console, typing the name of a variable (e.g., window) prints out its information. Running any void function (like console.log()) from the console also displays information about the return value, which is "undefined" in this case.
It has been confirmed that this behavior occurs in Chrome (version 23.0.1271.101) on Mac systems. Additionally, the "undefined" line also appears when directly assigning a value to a variable in the console.
The above is the detailed content of Why Does `console.log()` Show 'undefined' in Browser Consoles?. For more information, please follow other related articles on the PHP Chinese website!