-2

What does the following code mean in JavaScript :

(function() {

})();
iJade
  • 20,206
  • 52
  • 141
  • 227

2 Answers2

0

Thats a singleton/IIFE (immediately invoked function expression).

Using an IIFE can be helpful when wanting to use a local scope which eliminates binding to global objects like the window.

There is also a slight performance benefit to this approach as you can pass in commonly used objects to the anonymous function. JavaScript first looks for a property in its local scope then works up the chain.

Simon Staton
  • 3,947
  • 2
  • 22
  • 47
0

It is a Self-Invoking Anonymous Function.

A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous.

More info

silentw
  • 4,479
  • 4
  • 22
  • 44
  • 4
    It's not self-invoking (the code calling it is not part of the function). It *is* immediately-invoked. The standard term is IIFE (immediately invoked function expression). – T.J. Crowder Jan 09 '15 at 12:11