Why does console.log say undefined, and then the correct value?
The console shows the return value of your input. console.log() doesn't return anything, so undefined.
You could just type directly into the console to get the result.
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
The undefined
is the return value of console.log()
. This is the standard behavior of Chrome, Safari, and Firefox JS Consoles.