What does it mean if console.log(4) outputs undefi

2019-01-05 05:06发布

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.

2条回答
老娘就宠你
2楼-- · 2019-01-05 05:47

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.

查看更多
爷的心禁止访问
3楼-- · 2019-01-05 05:58

I've tested it and even with a preset variable it did not work in my Safari:

i = 2;
console.log(i);

This seems to explain the bug that WebKit (engine of both Chrome and Safari) has: Link

查看更多
登录 后发表回答