Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Conventions JS References

JS Versions

JS Versions

JS HTML DOM

JS HTML DOM

JS Events

JS Events

JS Projects

JS Projects

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


Immediately Invoked Expressions

JavaScript IIFE

An Immediately Invoked Function Expression (IIFE) is a function that runs immediately after it is defined.

It is executed immediately, without being called.

What Is an IIFE?

Normally, a function runs only when it is called.

An IIFE runs automatically when the JavaScript engine reads it.

Example

(function () {
  console.log("Hello World");
})();

The function above is defined and executed at the same time.


Why Use an IIFE?

IIFEs were commonly used to avoid creating global variables.
Everything inside an IIFE is private to that function.

Example

(function () {
  let x = 10;
  console.log(x);
})();

// x is not accessible here

How an IIFE Works

The parentheses tell JavaScript to treat the function as an expression.
The final () runs the function immediately.

Syntax

(function () {
  // code here
})();

IIFE with Parameters

You can pass arguments into an IIFE.

Example

(function (name) {
  console.log("Hello " + name);
})("John");

IIFE with Return Value

An IIFE can return a value, which can be stored in a variable.

Example

let result = (function () {
  return 5 + 5;
})();

Arrow Function IIFE

Arrow functions can also be used as IIFEs.

Example

(() => {
  console.log("Hello World");
})();

IIFE in Modern JavaScript

Today, JavaScript modules often replace the need for IIFEs.
Modules have their own scope by default.

Example

<script type="module">
let x = 10;
</script>

The variable x is scoped to the module and not global.


When is IIFE Used?

  • To create a private scope
  • To run setup code once
  • In older JavaScript code

When Not to Use IIFE

  • When using modern JavaScript modules
  • When simpler code is sufficient

Common Mistakes

  • Forgetting the final parentheses
    Without (), the function will not run.
  • Confusing IIFE with normal functions
    IIFEs cannot be called again.
  • Overusing IIFEs
    Modern JavaScript often does not need them.

End of Functions Section

You have now learned the most important concepts about JavaScript functions.
Try the quiz to test your knowledge.

JavaScript Functions Quiz

Tip: Try this quiz question now:
Why were IIFEs commonly used before JavaScript modules existed?


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->