js-javascript-logo

Using immediately invoked function expression

By @rishantagarwal

Called as "Iffy" ( IIFE - immediately invoked function expression) is an anonymous function expression that is immediately invoked and has some important uses in Javascript.


(function() {
 // Do something​
 }
)()

It is an anonymous function expression that is immediately invoked, and it has some particularly important uses in JavaScript.

The pair of parenthesis surrounding the anonymous function turns the anonymous function into a function expression or variable expression. So instead of a simple anonymous function in the global scope, or wherever it was defined, we now have an unnamed function expression.

Similarly, we can even create a named, immediately invoked function expression:

(someNamedFunction = function(msg) {
	console.log(msg || "Nothing for today !!")
	}) (); // Output --> Nothing for today !!​

someNamedFunction("Javascript rocks !!"); // Output --> Javascript rocks !!
someNamedFunction(); // Output --> Nothing for today !!​

For more details, check the following URL’s -

  1. Link 1
  2. Link 2

Performance: jsPerf

  • js-javascript-share
  • js-javascript-share-twitter
  • js-javascript-share-facebook
  • js-javascript-share-linkedin
jstips book
MEET THE NEW JSTIPS BOOK You no longer need 10+ years of experience to get your dream job.

Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.

GET THE BOOK NOW jstips amazon
jstips book
MEET THE NEW JSTIPS BOOK The book to ace the JavaScript Interview.

A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.

GET THE BOOK NOW jstips amazon

Related tips

Please enable JavaScript to view the comments powered by Disqus.

AltStyle によって変換されたページ (->オリジナル) /