DEV Community

Mahin Tazuar
Mahin Tazuar

Posted on

IIFE - Immediately invoked function expression

Image descriptionIn javascript, we can call function Immediately like the below code. we know every function needs to call after determine. but in this case, we can call the function togetherly.

(function () {
    console.log('hello')
})();

// example 2

let x = (function(){
  return "my";
})();
console.log(x)
Enter fullscreen mode Exit fullscreen mode

Remember Expression means its produce value and it will be inside first brackets.
Sometimes we need a local clouser environment because we know if we use multiple variables with the same name its have many possibilities for conflict with each variable. So if we are using the same name variable within a function expression it has no chance to replace the value or some things. Because the IIFE function creates a local clouser where the variable data is saved then we can use it anywhere.

Discussion (0)