What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?

An (IIFE) Immediately Invoked Function Expression is a function that executed as soon as it’s defined.
It’s a common design pattern that helps create a private scope and avoiding polluting the global scope.

(function () {
// Code inside IIFE
})();…


This content originally appeared on DEV Community and was authored by Heru Hartanto

An (IIFE) Immediately Invoked Function Expression is a function that executed as soon as it’s defined.
It’s a common design pattern that helps create a private scope and avoiding polluting the global scope.

(function () {
  // Code inside IIFE
})();

The function declare inside parentheses and trailing () immediately invoke it.

This can be useful when you need to fetch data immediately after the page loads, here the example:

(async function () {
  try {
    let response = await fetch("https://jsonplaceholder.typicode.com/posts");
    let data = await response.json();
    console.log(data);
  } catch (error) {
    console.error("Error fetching data:", error);
  }
})();


This content originally appeared on DEV Community and was authored by Heru Hartanto


Print Share Comment Cite Upload Translate Updates
APA

Heru Hartanto | Sciencx (2024-10-13T03:39:38+00:00) What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?. Retrieved from https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/

MLA
" » What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?." Heru Hartanto | Sciencx - Sunday October 13, 2024, https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/
HARVARD
Heru Hartanto | Sciencx Sunday October 13, 2024 » What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?., viewed ,<https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/>
VANCOUVER
Heru Hartanto | Sciencx - » What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/
CHICAGO
" » What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?." Heru Hartanto | Sciencx - Accessed . https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/
IEEE
" » What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care?." Heru Hartanto | Sciencx [Online]. Available: https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/. [Accessed: ]
rf:citation
» What is an IIFE (Immediately Invoked Function Expression) and Why Should You Care? | Heru Hartanto | Sciencx | https://www.scien.cx/2024/10/13/what-is-an-iife-immediately-invoked-function-expression-and-why-should-you-care/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.