简体   繁体   中英

Does IIFE always return undefined?

In the code snipped below, why the content of variable foo is undefined? I was expecting our IIFE statement return something!

var foo = (function (){
    var a=8;
    console.log("hi");
})();

The function inside of your expression that you are invoking is not returning any values, so that is why foo is undefined. If it returned a value, then foo would contain that value.

var foo = (function(){
    var a = 8;
    console.log("hello");
    return "world";
})()
console.log(foo);//world

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