how do you create a function in javascript? code example

Example 1: functions in javascript

function name( parameter1 ,parameter 2,..)  //name signifies funcname
{
  
}

Example 2: function javascript

var x = myFunction(4, 3);     // Function is called, return value will end up in x

function myFunction(a, b) {
    return a * b;             // Function returns the product of a and b
}

Example 3: 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 4: declare function javascript

function myFunction(var1, var2) {
  return var1 * var2;
}

Example 5: define function in javascript

/* Declare function */
function myFunc(param) {
  // Statements 
}

Example 6: create function javascript

function myFunction(p1, p2) {
  return p1 * p2;   // The function returns the product of p1 and p2
}