How to write an IIFE from scratch

In his post The anatomy of an immediately-invoked function expression, my friend Chris Ferdinandi shows you how he writes an IIFE from scratch. Here’s another way to approach it.

I start with a leading semicolon, two sets of parentheses, and a closing semicolon.

;()();

Then, I take my anonymous function:

function () {

}

And drop it into the first set of parentheses:

;(function () {

})();

Boom. You’re good to go!

I also opt into strict mode to make my IIFE more bulletproof:

;(function () {

  "use strict";

  // Your code goes here...

})();

In addition to Chris’ resources, I recommend the following:


If you have questions, feedback, or any other suggestions, please do email me. I'd love to hear from you!