Table of Contents
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>

Result View Example
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>

Result View Example
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>

Result View Example
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>

Result View Example
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>

Result View Example
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>

Result View Example
Document in project
You can Download PDF file.