The purpose of the named function is that we can define it in the code and then call it when we need it by referencing it's name and passing some arguments to it. Named functions are useful if we need to call a function many times to pass different values to it or run it several times. Here is an example:
//JS function with name
function findBiggestFraction(a, b) {
var result;
a > b ? result = ["firstFraction", a] : result = ["secondFraction", b];
return result;
}
var firstFraction = 3 / 4;
var secondFraction = 5 / 7;
var fractionResult = findBiggestFraction(firstFraction, secondFraction);
console.log("First fraction result: ", firstFraction);
console.log("Second fraction result: ", secondFraction);
console.log("Fraction " + fractionResult[0] + " with the value of " + fractionResult[1] + " is the biggest");
The anonymous functions don't have names. They need to be tied to something: variable or an event to run. The same function from above but with anonymous function:
//JS anonymous function without a name
var theBiggest = function (a, b) {
var result;
a > b ? result = ["a", a] : result = ["b", b];
return result;
}
console.log(theBiggest(3 / 4, 5 / 7));
Invoked function expression runs as soon as the browser encounters it. The benefit of this function is that it runs immediately where it's located in the code and produces a direct output. That means it is unaffected by code which appears further down in the script which can be useful. These functions are great for quickly populating a variable or argument in a larger function or a property in an object and are often hooked to event listeners for an immediate output.
The same function from above but with immediately invoked funcitonal expression:
var theBiggest = (function (a, b) {
var result;
a > b ? result = ["a", a] : result = ["b", b];
return result;
})(3 / 4, 5 / 7)
console.log(theBiggest);