admin管理员组

文章数量:1333451

Take this simple Test object and paste it into the console. You'll see that it says undefined. The object is working because it also prints 123, but what is the undefined about?

Test:

var Test = new (function(){
    return {
        get testing(){
            return "123";
        }
    }
});

console.log(Test.testing);

Console Output:

123
undefined

Take this simple Test object and paste it into the console. You'll see that it says undefined. The object is working because it also prints 123, but what is the undefined about?

Test:

var Test = new (function(){
    return {
        get testing(){
            return "123";
        }
    }
});

console.log(Test.testing);

Console Output:

123
undefined
Share Improve this question edited Oct 20, 2020 at 17:16 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 10, 2012 at 13:25 DrahcirDrahcir 12k25 gold badges88 silver badges128 bronze badges 1
  • Possible duplicate: Why does this JavaScript code print "undefined" on the console? – Peter Mortensen Commented Oct 20, 2020 at 17:18
Add a ment  | 

2 Answers 2

Reset to default 5

That is the return value of console.log.

Try

console.log(1);

which gives

1
undefined

However, if you type just

Test.testing

that gives only

"123"

undefined is the return value from the console.log call.

本文标签: javascriptWhy does Chrome amp FireFox console print 39undefined39Stack Overflow