This project shows how to use a self-invoked function (also called IIFE) in JavaScript.
It is a simple and clean example for beginners to understand how this structure works.
A self-invoked function is a function that runs immediately after it is created.
You donβt need to call it manually. It runs by itself.
(() => {
// Your code here
})();- Keeps your code private and safe
- Stops global variable pollution
- Helps organize JavaScript code better
- Good for creating modules
The JavaScript file includes:
| Function | What it does |
|---|---|
self.init() |
Runs when the page is ready |
self.reset() |
Clears or resets the UI |
self.buildCSS() |
Adds CSS styles |
self.buildHTML() |
Adds HTML elements to the page |
self.setEvents() |
Adds click or input events |
self.fetchData() |
Gets data from an API (using async/await) |
self.saveToStorage() |
Saves data to localStorage in the browser |
All code lines include easy comments so you can learn how it works step by step.
- Open the HTML file in your browser
- Open the developer console (F12 or right click β Inspect β Console)
- Watch how the function runs automatically
JavaScript self-invoked function, JavaScript IIFE example, beginner JavaScript structure,
how to organize JavaScript code, self-invoking function explained, JavaScript module pattern,
$(document).ready with IIFE
- Building widgets or UI components
- Creating isolated modules
- Writing plugin-like behavior without polluting the global scope
This example was created for beginners who want to understand IIFE in JavaScript.
You are free to use, fork, or improve it.
β If you find this useful, feel free to star the repo!