Home > Web Front-end > JS Tutorial > Why Does `console.log()` Show 'undefined' in the Browser Console?

Why Does `console.log()` Show 'undefined' in the Browser Console?

Mary-Kate Olsen
Release: 2024-12-15 18:24:11
Original
154 people have browsed it

Why Does `console.log()` Show

Undefined Tag in Console Output Logs

When executing console.log() in both Firefox and Chrome browsers (tested on Windows and Linux), users may encounter an additional line in their output logs stating "undefined." This article investigates the cause of this issue.

Explanation

The undefined tag appears in output logs when running console.log() from the console itself. This occurs because the console provides information about variables and return values when they are entered. When executing void functions like console.log(), the console also prints the return value, which in this case is undefined.

In contrast, running console.log() from a JS file does not result in the undefined tag being appended to the logs.

Example

To demonstrate this behavior, open the console tab in Chrome or Firefox and enter:

var bla = "sdfdfs"
Copy after login

You will notice the undefined tag appended to the output. This is because the assignment to the variable bla is a void function.

It is important to note that this behavior is not a bug or error. The undefined tag appears as a way to provide additional information about the return value of functions executed from the console.

The above is the detailed content of Why Does `console.log()` Show 'undefined' in the Browser Console?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template