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

JavascriptDebuggingLoggingConsole

Javascript Problem Overview


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 and Linux.

Javascript Solutions


Solution 1 - Javascript

If you're running console.log() from a JS file, this undefined line should not be appended.

If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.

I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"

Solution 2 - Javascript

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:

Solution 3 - Javascript

Follow the picture to solve this problem:

Ctrl + Shift + J

Solution 4 - Javascript

Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.

The result of an assignment expression is the value that was assigned. So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:

here.

In statements that have no return value you get something like undefined.

Solution 5 - Javascript

What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:

console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."

Solution 6 - Javascript

undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.

> let x = 3
> undefined

Solution 7 - Javascript

That undefined you see in console is the return value of the function: check out these two variants: >This one returns nothing

enter image description here


> This one returns something:

enter image description here

Solution 8 - Javascript

If you're using console.log to emit multiple values in a single line, here's a hacky alternative:

var1 + ' ' + var2 + ' ' + var...

(Better ideas welcome, this might blow up in certain circumstances)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionN. ChamaaView Question on Stackoverflow
Solution 1 - JavascripttalkolView Answer on Stackoverflow
Solution 2 - JavascriptPaul Vincent BeigangView Answer on Stackoverflow
Solution 3 - JavascriptPhilippe PiheynsView Answer on Stackoverflow
Solution 4 - JavascriptMahvash FatimaView Answer on Stackoverflow
Solution 5 - JavascriptScepticView Answer on Stackoverflow
Solution 6 - JavascriptYigit AlparslanView Answer on Stackoverflow
Solution 7 - JavascriptMhmdrz_AView Answer on Stackoverflow
Solution 8 - Javascripti336_View Answer on Stackoverflow