javascript how to make a function code example
Example 1: js function
function name(parameter1, parameter2, parameter3) {
// what the function does
}
Example 2: how to make a function in javascript
// Code by DiamondGolurk
// Defining the function
function test(arg1,arg2,arg3) {
// Insert code here.
// Example code.
console.log(arg1 + ', ' + arg2 + ', ' + arg3)
}
// Running the function
test('abc','123','xyz');
// Output
// abc, 123, xyz
Example 3: How to create a function in javascript
function addfunc(a, b) {
return a + b;
// standard long function
}
addfunc = (a, b) => { return a + b; }
// cleaner faster way creating functions!
Example 4: 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);