iife javascript example

Example 1: immediate invoke function js

(function () {
    statements
})();

Example 2: iife javascript

var result = (function () {
    var name = "Barry"; 
    return name; 
})(); 
// Immediately creates the output: 
result; // "Barry"

Example 3: javascript Immediately Invoked Function Expression

result = (function(a, b){
    return a - b;
})(100, 42);

console.log(result); // 58

Example 4: immediate invoke function js

(() => {
  // statements
})();

Example 5: immediately invoked function expression

(function () {
    var aName = "Barry";
})();
// Variable aName is not accessible from the outside scope
aName // throws "Uncaught ReferenceError: aName is not defined"