Self–invoking function in JavaScript?

The self-invoking function in JavaScript are known as Immediately Invoked Function Expressions (IIFE). Immediately Invoked Function Expressions (IIFE) is a JavaScript function that executes immediately after it has been defined so there is no need to manually invoke IIFE.

Following is the code for self-invoking function (IIFE) in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="javascript:void(0);en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .sample {
      font-size: 18px;
      font-weight: 500;
<h1>JavaScript Immediately Invoked Function Expressions (IIFE)</h1>
<div class="sample"></div>
   let sampleEle = document.querySelector(".sample");
   (function () {
      sampleEle.innerHTML = "This code is invoked immediately as soon as it is defined";


The above code will produce the following output −