95

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
N. Chamaa
  • 1,507
  • 3
  • 12
  • 22

9 Answers9

117

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"

talkol
  • 12,564
  • 11
  • 54
  • 64
  • Thanks for replying.I'm running it from the console itself, I tried to stop all the extensions , but I get the same result. – N. Chamaa Jan 31 '13 at 20:17
  • 3
    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 – talkol Jan 31 '13 at 20:24
  • but it wasn't before , that's why!! – N. Chamaa Jan 31 '13 at 20:40
  • 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.. – talkol Jan 31 '13 at 20:47
44

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:

Community
  • 1
  • 1
Paul Vincent Beigang
  • 2,972
  • 2
  • 16
  • 31
  • 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. – Dan Feb 11 '14 at 20:37
  • 6
    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. – Paul Vincent Beigang Feb 15 '14 at 14:54
  • 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 :) – Dan Feb 15 '14 at 21:18
16

Follow the picture to solve this problem:

Ctrl + Shift + J

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
8

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Maybe a little bit off-topic but why does `a=2` return 2 and `var b=2` return nothing? (I guess because `=` is an operator that returns a value but why doesn't it do the same in the declaration statement?) – floatingpurr Dec 17 '19 at 16:32
  • 1
    @Fatima, What is your reference for this answer? – Mr. Perfectionist Dec 06 '20 at 09:32
2

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

Mechanic
  • 5,015
  • 4
  • 15
  • 38
1

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."
Sceptic
  • 1,659
  • 2
  • 15
  • 25
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yigit Alparslan
  • 1,377
  • 1
  • 8
  • 13
0

Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.

Rahul Daksh
  • 212
  • 1
  • 7
-4

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)

i336_
  • 1,813
  • 1
  • 20
  • 41