

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 JavaThey can also return values and take arguments:
const result = (function (a, b) {
return a + b;
})(1, 2);
console.log(result);
// 3The function is defined and then immediately called. It looks nasty, but it's occasionally useful for a couple of reasons:
Scope: It has its own scope
Expression: Can be convenient for computing a value as a single expression (like above)
Async: Can be used to quickly run code in an 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: 100
bytesPerMessage: 24