Immediately Invoked Function Expressions (IIFE) - Javascript Function

Javascript examples for Function:Immediately Invoked Function Expressions IIFE

Description

Immediately Invoked Function Expressions (IIFE)

Demo Code

// Normal way://from  w  w w  . j a  va  2s .  c o m
function game() {
  var score = Math.random () * 10;
  console.log(score >= 5);
}

game();
// IIFE way:
(function() {
  var score = Math.random () * 10;
  console.log(score >= 5);
})();

// IIFE, with parameters and arguments:
(function(goodLuck) {
  var score = Math.random () * 10;
  console.log(score >= 5 - goodLuck);
})(5);

Related Tutorials