JavaScript » Statements » function

function name([param] [, param] [..., param]) {statements}

The function statement declares a function with its specified parameters, which can include numbers, strings and objects. To return a value, a function must have a return statement specifying the value to return.

The word, function, is a reserved word. You cannot name a function, "function". Rather, use a word, or blend of words, that describe the purpose that the function performs.


function calcaverage(a, b, c)
   return (a+b+c)/3;
document.write(calcaverage(2, 4, 6));

This code declares a function that calculates the average of three numbers and returns the result: