By N. Chamaa

2013-01-31 19:50:43 8 Comments

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

Happens in both Firefox and Chrome on Windows and Linux.


@Mahvash Fatima 2018-10-20 05:40:50

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:


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

@R.. 2018-06-11 19:13:22

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.
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."

@Philippe Piheyns 2018-03-30 16:10:22

Follow the picture to solve this problem:

Ctrl + Shift + J

@Paul Vincent Beigang 2014-02-11 20:19:26

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.]


@Dan 2014-02-11 20:37:10

Referencing other posts is fine, but this isn't a full answer so it would fit better as a comment on the answer you are referencing.

@Paul Vincent Beigang 2014-02-15 14:54:04

I tried to do that, but I am not able to comment due to lack of enough reputation ("You must have 50 reputation to comment"), so I thought using the "unoptimal" way of answering instead of commenting would be better then not posting.

@Dan 2014-02-15 21:18:48

That's fair. I can't remove my downvote without you editing the post though. Mind elaborating on what is located at that link in the answer? If you can do that, then I can change my vote :)

@Paul Vincent Beigang 2014-02-16 20:36:42

Thanks Dan for your patience, done that, ok? :)

@i336_ 2016-03-29 03:14:35

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)

@talkol 2013-01-31 19:57:01

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"

@N. Chamaa 2013-01-31 20:17:25

Thanks for replying.I'm running it from the console itself, I tried to stop all the extensions , but I get the same result.

@talkol 2013-01-31 20:24:34

Well, this is the expected behavior. Everything is working as it should. You can enable you extensions back :) Just run console.log from a JS file and you won't see this. Why are you running console.log from the console anyways? You can just type any variables name without console.log

@N. Chamaa 2013-01-31 20:40:06

but it wasn't before , that's why!!

@talkol 2013-01-31 20:47:52

Maybe your Chrome was updated and this behavior started in newer versions.. Since the same thing happens in my Chrome (on Mac), I highly doubt it's a problem..

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] Can't access object property, even though it exists. Returns undefined

10 Answered Questions

[SOLVED] Save the console.log in Chrome to a file

9 Answered Questions

8 Answered Questions

[SOLVED] How to log PostgreSQL queries?

  • 2009-04-06 16:34:32
  • Paul
  • 341514 View
  • 328 Score
  • 8 Answer
  • Tags:   postgresql logging

26 Answered Questions

[SOLVED] Storing Objects in HTML5 localStorage

2 Answered Questions

4 Answered Questions

[SOLVED] console.log in firefox is not working?

  • 2013-03-26 09:25:44
  • Ashraf Bashir
  • 62322 View
  • 54 Score
  • 4 Answer
  • Tags:   firefox logging

12 Answered Questions

5 Answered Questions

[SOLVED] Javascript Console.log Not working in Chrome, or Firefox

6 Answered Questions

[SOLVED] What's the difference between console.dir and console.log?

Sponsored Content