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
Explanation of 'Let' and Block Scoping With For Loops
How to Get the Browser Viewport Dimensions
What Is Destructuring Assignment and Its Uses
Format JavaScript Date as Yyyy-Mm-Dd
Large Numbers Erroneously Rounded in JavaScript
Checking If a Key Exists in a JavaScript Object
How to Print a Circular Structure in a Json-Like Format
Detecting an "Invalid Date" Date Instance in JavaScript
Regular Expression to Match Non-Ascii Characters
How to Convert a String to Boolean in JavaScript
Determine Whether an Array Contains a Value
Better Way to Sum a Property Value in an Array
Why Doesn't My Arrow Function Return a Value
Get Selected Text from a Drop-Down List (Select Box) Using Jquery
How to Check For an Empty/Undefined/Null String in JavaScript