JS Coding Questions Logo
JS Coding Questions
#23💼 Interview💻 Code

What is an IIFE (Immediately Invoked Function Expression)

Advertisement

728x90

IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below,

javascript
1(function () {
2  // logic here
3  })();

The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables from the IIFE then it throws an error as below,

javascript
1(function () {
2
3var message = "IIFE";
4
5console.log(message);
6})();
7
8console.log(message); //Error: message is not defined

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 25

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
23of476
What is an IIFE (Immediately Invoked Function Expression) | JSCodingQuestions.com