Skip to content

Commit

Permalink
Update IIFE Example and Related detail
Browse files Browse the repository at this point in the history
Listed extra explanation for further exploration and added the benefits.
  • Loading branch information
shivvamm authored Jul 23, 2023
1 parent 5e00578 commit f12410e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions files/en-us/glossary/iife/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The name IIFE is promoted by Ben Alman in [his blog](https://web.archive.org/web
//
})();
```
The IIFE syntax involves wrapping the function in parentheses and immediately invoking it with an additional set of parentheses. This structure ensures that the function is treated as an expression, and the final pair of parentheses triggers its immediate execution.

It is a design pattern which is also known as a {{glossary("Self-Executing Anonymous Function")}} and contains two major parts:

Expand Down Expand Up @@ -152,6 +153,14 @@ console.log(i); // Uncaught ReferenceError: i is not defined.

When clicked, these buttons alert 0 and 1.

##Benefits of IIFE

Encapsulation: Variables and functions defined within the IIFE are local to the function's scope, preventing unintentional global variable declarations.

Minimization of Global Scope: By enclosing code within an IIFE, you limit its accessibility to the rest of the application, reducing the risk of naming collisions.

Initialization: IIFEs are often used to initialize scripts and set up configurations before the main application starts running.

## See also

- [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) (Wikipedia)
Expand Down

0 comments on commit f12410e

Please sign in to comment.