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