Chrome/Firefox Console.Log Always Appends a Line Saying 'Undefined'

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

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"

What does it mean if console.log(4) outputs undefined in Chrome Console?

The undefined is the return value of console.log(...).

You can see this by defining two functions in the console, one returning something, and the other returning nothing, e.g. like this:

function f1() {
return 1;
}
function f2() {
return;
}

And then calling them separately (manually)

f1(); // shows '1'

and

f2(); // shows 'undefined'

Also note the little symbol before these return value string.

Why does console.log say undefined, and then the correct value?

The console will print the result of evaluating an expression. The result of evaluating console.log() is undefined since console.log does not explicitly return something. It has the side effect of printing to the console.

You can observe the same behaviour with many expressions:

> var x = 1;
undefined;

A variable declaration does not produce a value so again undefined is printed to the console.

As a counter-example, expressions containing mathematical operators do produce a value which is printed to the console instead of undefined:

> 2 + 2;
4

Why the following code gives 'undefined' inside output

This is because return value of the function is undefined. So it first log 1,4,return value of function then it will log the values from setTimeout

For example

(function(){
console.log(1);
setTimeout (function(){console.log(2);},1000);
setTimeout (function(){console.log(3);},0);
console.log(4);
return 'xyz'

})();

Output will be

1,4,'xyz',3,2

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

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"

Why does my forloop + if/else console log an undefined value

This undefined is not coming from the console.log. It's the return value of the function, since function is not returning anything.

For example, in repl.it:

let a
let b
console.log(a + " " + b)

prints:

undefined undefined
=> undefined

Not that the console.log would have printed 2 undefines if it were part of that loop.



Related Topics



Leave a reply



Submit