Skip to main content
InterviewHubPRO
Section Overview
Q26

Question 26 of 536

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

5%
JavaScript & V8 Internals (536 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

What is an IIFE (Immediately Invoked Function Expression)

junior LevelMost AskedCode ExamplePrototypes

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
(function () {
  // logic here
})();

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
(function () {
  var message = "IIFE";
  console.log(message);
})();
console.log(message); //Error: message is not defined