1

Both of these functions below are self-invoking functions. Can anyone explain to me what is difference between these two? I've looked around a lot of places. But I've not been able to find anything.

First Type

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

Second Type

(function(){
    console.log('Hello world');
})();
p.matsinopoulos
  • 7,506
  • 6
  • 42
  • 92
Kaushik
  • 2,024
  • 1
  • 21
  • 29

1 Answers1

4

They're the same. They're just two different but similar ways to force the JS engine correctly interpret the function expression.

Another way would be for example

+function(){
    console.log('Hello world');
}() 

The most usually accepted convention is to put the parenthesis around the function expression :

(function(){
    console.log('Hello world');
})();
Denys Séguret
  • 355,860
  • 83
  • 755
  • 726