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.
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.
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.
The undefined property indicates that a variable has not been assigned a value, or not declared at all.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With