简体   繁体   中英

When I console.log type of my function which returns an object it gives undefined. I don't understand why

I can't understand why the result of this code is undefined?

function f() {
    return
    {
        x: 0
    };
}
console.log(type of f());

typeof is a single word. Also, you are invoking your function, thus you are evaluating it's returned value, which is undefined in your code because of automatic semicolon insertion.

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

The return statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.

 function f() { return //this is treated as though it had a ; { x: 0 }; } console.log(typeof f()); console.log(typeof f); 

 function f() { return { x: 0 }; } console.log(typeof f()); console.log(typeof f); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM