JavaScript IIFE

Back to JS page

Table of Contents

What Is an IIFE?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>What Is an IIFE?</h4>
<p id="demo"></p>

<script>
(function() {
  document.getElementById("demo").innerHTML = "IIFE executed!";
})();
</script>

</body>
</html>

Example 1

Result View Example

Avoid Polluting the Global Scope

IIFEs are commonly used to avoid polluting the global namespace. Variables declared inside an IIFE are not accessible outside:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Avoid Polluting the Global Scope</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
(function() {
  let privateVar = "This is private";
  document.getElementById("demo1").innerHTML = "Inside IIFE: " + privateVar;
})();

// privateVar is NOT accessible here (would cause error)
document.getElementById("demo2").innerHTML = "Outside IIFE: privateVar is not accessible";
</script>

</body>
</html>

Example 2

Result View Example

IIFE with Parameters

IIFEs can accept arguments, just like regular functions:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>IIFE with Parameters</h4>
<p id="demo"></p>

<script>
(function(name, age) {
  document.getElementById("demo").innerHTML = "Hello " + name + ", you are " + age + " years old.";
})("John", 30);
</script>

</body>
</html>

Example 3

Result View Example

IIFE with Return Value

An IIFE can return a value that is assigned to a variable:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>IIFE with Return Value</h4>
<p id="demo"></p>

<script>
let result = (function() {
  let x = 5;
  let y = 10;
  return x * y;
})();

document.getElementById("demo").innerHTML = "Result: " + result;
</script>

</body>
</html>

Example 4

Result View Example

Arrow Function IIFE

IIFEs can also be written using arrow function syntax:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Arrow Function IIFE</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
// Arrow function IIFE
(() => {
  document.getElementById("demo1").innerHTML = "Arrow IIFE executed!";
})();

// Arrow function IIFE with parameter
((message) => {
  document.getElementById("demo2").innerHTML = "Message: " + message;
})("Hello from arrow IIFE!");
</script>

</body>
</html>

Example 5

Result View Example

IIFE as a Module (Private Variables)

IIFEs can be used to create modules with private variables and public methods:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript IIFE</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>IIFE as a Module</h4>
<p id="demo"></p>

<script>
const counterModule = (function() {
  let count = 0;  // private variable

  return {
    increment: function() {
      count++;
      return count;
    },
    decrement: function() {
      count--;
      return count;
    },
    getCount: function() {
      return count;
    }
  };
})();

document.getElementById("demo").innerHTML =
  "Initial: " + counterModule.getCount() + "<br>" +
  "After increment: " + counterModule.increment() + "<br>" +
  "After increment: " + counterModule.increment() + "<br>" +
  "After decrement: " + counterModule.decrement();
</script>

</body>
</html>

Example 6

Result View Example

Document

Document in project

You can Download PDF file.

Reference