what are functions used for in javascript code example

Example 1: functions in javascript

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

Example 2: how to use function in javascript

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

Example 3: js function

function myfunction() {
  console.log("function");
};

myfunction() //Should say "function" in the console.

function calculate(x, y, op) {
  var answer;
  if ( op = "add" ) {
    answer = x + y;
  };
  else if ( op = "sub" ) {
    answer = x - y;
  };
  else if ( op = "multi" ) {
    answer = x * y;
  };
  else if ( op = "divide" ) {
    answer = x / y;
  };
  else {
    answer = "Error";
  };
  return answer;
};

console.log(calculate(15, 3, "divide")); //Should say 5 in console.
//I hope I helped!

Example 4: javascript functions

function sayHelloTo(to) {
  alert(`Hello ${to}`);
}
sayHelloTo('World from Grepper')