I am trying to understand what is happening when I call console.log within another console.log(), like so:
console.log(console.log("Hello"));
My results are:
Hello
undefined
What is the outer console.log() trying to show that is undefined?
I am trying to understand what is happening when I call console.log within another console.log(), like so:
console.log(console.log("Hello"));
My results are:
Hello
undefined
What is the outer console.log() trying to show that is undefined?
The first call of console.log("Hello")
prints "Hello" and returns undefined
value to the next call. Thus the order is
Hello // from console.log("Hello");
undefined // from console.log(undefined);
The return value of console.log("Hello") : "void" is about the same as "undefined" in javascript
console.log()
returns nothing, it just prints to the console. Thus, you are trying to log
an undefined value.
As you can see below, we mimic the behavior of console.log
with a document.write
. The second value is undefined, as expected:
document.write(document.write("Hello"));
Basically
typeof console = "object"
typeof console.log = "function"
typeof console.log("Hello") = "undefined"
So the first console.log(x); has an undefined where there is an x.
Its because you are writing this code in console
and console.log()
is not return any value.
if you write same code in any function then you get different result
for E.g
function test()
{
console.log("test")
return 1;
}
Now when you execute test()
function in console you will get
test
1
and if you define another function with not return type..
function test2()
{
console.log("test")
}
Then you get
test
undefined
If a JavaScript method does not have a explicit return then it returns undefined
object. console.log according to chrome looks like:
console.log(object [, object, ...])
So, the inner console.log('Hello') //Print output browser console but it returns undefined
so outer console.log is printing undefined
.