js functions in html code example

Example 1: js function

function name(parameter1, parameter2, parameter3) {
// what the function does
}

Example 2: javascript function

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// FUNCTION DECLARATION (invoking can be done before declaration)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function calcAge1(birthYear) {
   return 2037 - birthYear;
}
const age1 = calcAge1(1991);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// FUNCTION EXPRESSION (invoking canNOT be done before declaration)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
const calcAge2 = function (birthYear) {
   return 2037 - birthYear;
}
const age2 = calcAge2(1991);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// ARROW FUNCTION (generally used for one-liner functions)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
const calcAge3 = birthYear => 2037 - birthYear;
const age3 = calcAge3(1991);
console.log(age3);

Tags:

Php Example