Anonymous functions
So far, we have been naming our functions. We can also create functions without names if we store them inside variables. We call these functions anonymous. Here is a non-anonymous function:
function doingStuffAnonymously() {
console.log("Not so secret though.");
}
Here is how to turn the previous function into an anonymous function:
function () {
console.log("Not so secret though.");
};
As you can see, our function has no name. It is anonymous. So you may wonder how you can invoke this function. Well actually, you can't like this!
We will have to store it in a variable in order to call the anonymous function; we can store it like this:
let functionVariable = function () {
console.log("Not so secret though.");
};
An anonymous function can be called using the variable name, like this:
functionVariable();
It will simply output Not so secret though.
.
This might seem a bit useless,...