JavaScript Tutorial JavaScript References

JavaScript - Function() Constructor



In JavaScript, by using function literals is another way of defining functions. A function literal is an expression that defines an unnamed function. The syntax for using function literals is given below:

Syntax

//Defining function
var variableName = function (parameters) {
  statements;
}

//Calling function
variableName(parameters);

Example:

The example below shows the usage of function literals.

var func = function (a, b){
  document.write ("The sum is ", a + b); 
}

func(10, 15);

The output of the above code will be:

The sum is 25

Example:

Consider one more example to describe the usage of function literals.

var func = function (a, b){
  document.write ("The product is ", a * b); 
}

function Myfunction() {
  func(5, 12);
}

Myfunction();

The output of the above code will be:

The product is 60

❮ JavaScript - Functions