function construct

The function construct creates a custom function. This function can then be called elsewhere in a code by passing some parameters to it.

Syntax

javascriptfunction functionName(parameters) {
  commands that a function executes.
};

Example

Let's create and call a function:

javascript// We create a function:
function func() {
  alert('!');
}

// We call a function:
func(); // shows '!'

Example

Let's create the function that takes two numbers as a parameter and displays their sum:

javascriptfunction func(num1, num2) {
  alert(num1 + num2);
}

func(1, 2); // shows 3

See also