Chrome/Firefox console.log always appends a line saying 'undefined'
Solution 1
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
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:
- https://stackoverflow.com/a/20915524/1744768
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Solution 3
Follow the picture to solve this problem:
Ctrl + Shift + J
Solution 4
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.
Solution 5
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
Related videos on Youtube
N. Chamaa
Updated on July 08, 2022Comments
-
N. Chamaa almost 2 years
Every time
console.log
is executed, a line sayingundefined
is appended to the output log.It happens in both Firefox and Chrome on Windows and Linux.
-
kapa over 11 yearsSo what is your question? Could you show some example code that produces this? What is the behaviour you expect?
-
Titouan de Bailleul over 11 yearsThat's probably not enough information to solve your problem
-
talkol over 11 yearsI gave it my best shot even though it's indeed lacking some info :)
-
gen_Eric over 11 years
console.log()
returnsundefined
. -
Leo over 5 yearsMy issue was I was only displaying the errors and warnings, displaying the messages and info worked for me.
-
-
N. Chamaa over 11 yearsThanks for replying.I'm running it from the console itself, I tried to stop all the extensions , but I get the same result.
-
talkol over 11 yearsWell, 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 over 11 yearsbut it wasn't before , that's why!!
-
talkol over 11 yearsMaybe 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..
-
Dan over 10 yearsReferencing 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 over 10 yearsI 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 over 10 yearsThat'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 :)
-
floatingpurr over 4 yearsMaybe a little bit off-topic but why does
a=2
return 2 andvar 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?) -
Peter Mortensen over 3 yearsFirefox or Chrome?
-
Mr. Perfectionist over 3 years@Fatima, What is your reference for this answer?