调用函数时得到未定义的结果

Getting undefined result when invoked function

当我在控制台中调用函数时记录它 returns undefined 值。我不确定这可能是什么原因。

function test() {
    var foo=1;
    {
        let boo=2;
        console.log(boo);
    }
    console.log(foo);
}

console.log(test());

输出

2
1
undefined

您正在尝试记录 test() 函数的 output/return 值。 由于没有 returned,它会将其作为未定义注销。

return 一些东西,你会看到它记录了一些东西:

    function test() {
        var foo=1;
        {
            let boo=2;
            console.log(boo);
        }
        console.log(foo);
        return "you should return something here"
    }
    
    console.log(test());