Creating Functions

Creating a function involves stating a function name and then adding a series of statements or other functions between curly braces. The general format is as follows:

						function functionName(optional arguments) {
   statements or other functions 
   } 

For example, the following function in the following script can be used to determine the number of 1 × 8 boards needed for a shed’s walls. The two arguments in the function, front and side, are used as variables in two prompt functions.

<html> 
<head> 
<title>Shed Planner</title> 
<script language="JavaScript"> 
function shedWalls(front,side) { var front=prompt("How many feet across the front?",""); var side=prompt("How many feet along the side?",""); var numFront=(front * 12 * 2)/8; var ...

Get JavaScript Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.