IIFE Pattern in JavaScript

Level 3114 keystrokes

IIFE (Immediately Invoked Function Expression) runs immediately and creates a private scope.

JavaScript code

// IIFE - function that runs immediately when defined
// Wrapped in parentheses and followed by () to call it
1const result = (() => {
// Variables inside are private to this scope
2 const a = 5;
3 const b = 10;
// Return value is assigned to result
4 return a + b;
5})();
// result is 15
6console.log(result);
// a is not defined outside the IIFE - prints "undefined"
7console.log(typeof a);

Output

15
undefined
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • ;× 6
  • (× 5
  • )× 5
  • =× 4
  • .× 2
  • {× 1
  • }× 1
  • +× 1
  • >× 1

More JavaScript examples at level 3

← JavaScript typing practice