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

50,936

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:

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:

here.

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
Share:
50,936

Related videos on Youtube

N. Chamaa
Author by

N. Chamaa

Updated on July 08, 2022

Comments

  • N. Chamaa
    N. Chamaa almost 2 years

    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.

    • kapa
      kapa over 11 years
      So what is your question? Could you show some example code that produces this? What is the behaviour you expect?
    • Titouan de Bailleul
      Titouan de Bailleul over 11 years
      That's probably not enough information to solve your problem
    • talkol
      talkol over 11 years
      I gave it my best shot even though it's indeed lacking some info :)
    • gen_Eric
      gen_Eric over 11 years
      console.log() returns undefined.
    • Leo
      Leo over 5 years
      My issue was I was only displaying the errors and warnings, displaying the messages and info worked for me.
  • N. Chamaa
    N. Chamaa over 11 years
    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
    talkol over 11 years
    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
    N. Chamaa over 11 years
    but it wasn't before , that's why!!
  • talkol
    talkol over 11 years
    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..
  • Dan
    Dan over 10 years
    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
    Paul Vincent Beigang over 10 years
    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
    Dan over 10 years
    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 :)
  • floatingpurr
    floatingpurr over 4 years
    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?)
  • Peter Mortensen
    Peter Mortensen over 3 years
    Firefox or Chrome?
  • Mr. Perfectionist
    Mr. Perfectionist over 3 years
    @Fatima, What is your reference for this answer?