简体   繁体   中英

Console.log does not work inside Self Executing Anonymous functions

(function() {
    console.log('Hello World');
})();

Does not print anything to console.

(function() {
    alert('Hello World');
})();

Gives an alert. Why so ?

Both alert and console are working, please check when you are using console.log in self executing function, you are calling that function by using these brackets () or not. May be you are missing these in your code -

**Incorrect Code -** 
(function() {
    console.log('Hello World');
});
**Correct Code -** 
(function() {
console.log('Hello World');
})();

Correct code

在此处输入图片说明

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