Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I used the Chrome Console to write a simple statement:

console.log(4)

and received the Output:

4

undefined

What does the undefined statement mean? Does the undefined statement imply correct execution? If I execute the statement via a separate html file and then look at the console, the output is just 4.

like image 489
AnthonyS Avatar asked Jun 19 '12 20:06

AnthonyS


People also ask

Why does Chrome console say undefined?

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.

Why is my console log undefined?

This is because console. log() does not return a value (i.e. returns undefined). The result of whatever you entered to the console is first printed to the console, then a bit later the message from console. log reaches the console and is printed as well.

What is undefined in console?

The undefined property indicates that a variable has not been assigned a value, or not declared at all.


1 Answers

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.

like image 149
Bart Avatar answered Sep 17 '22 15:09

Bart