Make a Function

Posted by David on March 13th, 2007 filed in JavaScript

A function is a named group of JavaScript statements that you can declare once, near the top of your script, and call over and over again.

Declaring a function

function name ([parameter] [, parameter] [..., paramenter]){
statements
return value
}

Call a function

alert(”Total purchases come to ” + calculateTotal (10, 19.85))

Returning a value from a function

function calculateTotal (numberOrdered, itemPrice){
var totalPrice = (numberOrdered * itemPrice) + salesTax
return totalPrice
}

The call function above will not get the result without the returning value.

Related posts

Comments are closed.