JavaScript IIFE

What is an IIFE?

IIFE stands for Immediately Invoked Function Expression.

(function() {
    // code
})();

Code like this means “run the code inside this function immediately.”

IIFE Example

The following code does not run unless write(); is executed separately.

function write() {
  document.write("Test");
}

write();

If you write the code as follows, it runs immediately.

(function write() {
  document.write("Test");
})();

References