

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Functions
incomplete
2: Function Hoisting
incomplete
3: Unit Test Lessons
incomplete
4: Multiple Return Values
incomplete
5: Functions As Values
incomplete
6: Scope
incomplete
7: Anonymous Functions
incomplete
8: Default Parameters
incomplete
9: Passing by Value
incomplete
10: Immediate Invocation
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
You can immediately invoke a function after defining it using the not-at-all-hard-to-pronounce acronym "IIFE" (Immediately Invoked Function Expression).
(function () {
console.log("JavaScript: at least it's not Java");
})();
// JavaScript: at least it's not Java
They can also return values and take arguments:
const result = (function (a, b) {
return a + b;
})(1, 2);
console.log(result);
// 3
The function is defined and then immediately called. It looks nasty, but it's occasionally useful for a couple of reasons:
async function (we'll cover this later)Run the given code. Notice that the total variable doesn't store the resulting number, but is actually a function!
Wrap it in an IIFE so that the total variable is calculated immediately using the following parameters:
numMessages: 100bytesPerMessage: 24