Earlier in this tutorial, you learned that functions are declared with the following syntax:

function functionName(parameters) {
  // code to be executed
}

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon).

Example

function myFunction(a, b) {
  return a * b;
}

Semicolons are used to separate executable JavaScript statements.
Since a function declaration is not an executable statement, it is not common to end it with a semicolon.


Function Expressions

A JavaScript function can also be defined using an expression.

A function expression can be stored in a variable:

Example

const x = function (a, b) {return a * b};
 

After a function expression has been stored in a variable, the variable can be used as a function:

Example

const x = function (a, b) {return a * b};
let z = x(4, 3);
 

The function above is actually an anonymous function (a function without a name).

Functions stored in variables do not need function names. They are always invoked (called) using the variable name.

The function above ends with a semicolon because it is a part of an executable statement.



Practice Excercise Practice now