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

Does not print anything to console.

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

Gives an alert. Why so ?

Bhumi Singhal
  • 8,063
  • 10
  • 50
  • 76

1 Answers1

2

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

enter image description here

Ayush Sharma
  • 2,057
  • 15
  • 25