javascript func code example
Example 1: javascript function
function myFunc(theObject) {
theObject.make = 'Toyota';
}
var mycar = {make: 'Honda', model: 'Accord', year: 1998};
var x, y;
x = mycar.make; // x gets the value "Honda"
myFunc(mycar);
y = mycar.make; // y gets the value "Toyota"
// (the make property was changed by the function)
Example 2: simple javascript function
function idk() {
alert('This is an alert!')
}
//call the function to make the function run by saying "Name of function + ()"
idk()
Example 3: javascript function
the function of javascript is to teach first year programers
synactically correct language constructs by way of an anti-pattern.
Example 4: how to use function in javascript
/* Declare function */
function myFunc(param) {
// Statements
}
Example 5: 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);