javascript method definition code example
Example 1: javascript function
// variable:
var num1;
var num2;
// function:
function newFunction(num1, num2){
return num1 * num2;
}
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);
Example 3: what is a javascript method
JavaScript methods are actions that can be performed on syntax.
Examples of methods include syntax ending with ();
Examples:
console.log();
toUpperCase();
Math.floor();
They help change original syntax in various ways.