← Back to JavaScript Course | Chapter 6: Functions Advanced | Lesson 5 of 8

JS IIFEs

An IIFE is a function that runs the moment it is made, like a firework that goes off as soon as you light it. It keeps its variables private from the rest of the code.
Syntax
javascript
(function() {
  // runs immediately
})();

(() => {
  // runs immediately
})();

What Is an IIFE?

IIFE means Immediately Invoked Function Expression. It runs as soon as it is created. Wrapping code in (function(){ ... })() executes it once immediately without leaving a named function sitting around afterward.

उदाहरण: What Is an IIFE?

javascript
(function () {
  // Print "Runs immediately" to the console
  // Print "Runs immediately" to the console
  console.log("Runs immediately");
})();

Why Use IIFEs?

An IIFE creates its own scope. Variables inside it do not become global variables. Before ES modules were standard, this was the primary way libraries avoided polluting the global namespace with their internal helper variables.

उदाहरण: Why Use IIFEs?

javascript
(function () {
  var secret = "hidden"; // not a global variable
  console.log(secret);
})();
console.log(typeof secret); // "undefined"

IIFE With Return

An IIFE can return a value immediately. Store that returned value in a variable. This pattern, const result = (function(){ ... return value; })(), lets you compute a value using local helper variables that don't leak outside the IIFE.

उदाहरण: IIFE With Return

javascript
const result = (function () {
  // Declare the constant `a`, set to `5, b = 10`
  // Declare the constant `a`, set to `5, b = 10`
  const a = 5, b = 10;
  // Return `a + b` from this function
  // Return `a + b` from this function
  return a + b;
})();
// Print `result` to the console
// Print `result` to the console
console.log(result);

IIFE With Parameters

An IIFE can accept arguments just like a normal function. For example, (function(name){ console.log(name); })(Alice) behaves like calling a regular function, just invoked exactly once at definition time.

उदाहरण: IIFE With Parameters

javascript
(function (name) {
  // Print `name` to the console
  // Print `name` to the console
  console.log(name);
})("Alice");

Modern Use

Modules are more common today, but IIFEs are still useful when you need an isolated scope in a script.

Even in module-based codebases, a small IIFE is still occasionally reached for when you need a self-contained block of setup logic that shouldn't expose its internals.

उदाहरण: Modern Use

javascript
const config = (function () {
  // Declare the constant `secret`, set to "key123"
  // Declare the constant `secret`, set to "key123"
  const secret = "key123";
  // Return `{ getKey: () => secret }` from this function
  // Return `{ getKey: () => secret }` from this function
  return { getKey: () => secret };
})();
// Print `config.getKey()` to the console
// Print `config.getKey()` to the console
console.log(config.getKey());
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.