Home

User Functions

Named Functions

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 a value of " + fractionResult[1] + " is the biggest!");

Console Result (F12)

First fraction result: 0.75
userFunction.js:14 Second fraction result: 0.7142857142857143
userFunction.js:15 Fraction: firstFraction with a value of 0.75 is the biggest!

Anonymous Functions

var aAnon = 5/7;
var bAnon = 18/25;

var theBiggestAnon = function (a, b)
 {
    var resultAnon;
    aAnon>bAnon
    ? resultAnon = ["aAnon", aAnon]
    : resultAnon = ["bAnon", bAnon];
return resultAnon } // Inside theBiggestAnon variable, there is an anonymous function console.log(theBiggestAnon(aAnon,bAnon));

Console Result (F12)

(2) ["bAnon", 0.72]
 0: "bAnon"
 1: 0.72
 length: 2
__proto__: Array(0)

Immediately Invoked Functional Expressions

We want the function itself to be returned to the variable, not the result of the function.
This is achieved by wrapping the whole function in parentheses
var aAnon = 5/7;
var bAnon = 18/25;

var theBiggestAnon = (function (a, b)
 {
    var resultAnon;
    aAnon>bAnon
    ? resultAnon = ["aAnon", aAnon]
    : resultAnon = ["bAnon", bAnon];
return resultAnon })(aAnon,bAnon) // Inside theBiggestAnon variable, there is an anonymous function console.log(theBiggestAnon);

Console Result (F12)

Returning the result: bImmediate,0.72
userFunction.js:53 Returning the function: function (a, b)
{     var resultImmediate; aImmediate>bImmediate ? resultImmediate = ["aImmediate", aImmediate] : resultImmediate = ["bImmediate", bImmediate]; return resultImmediate }