Passing a Value to a Function

You’ll often want to take some information and give it to a function to use. This is called passing the information to the function. For example, look at this function definition:

function playBall(batterup)

The variable batterup is a parameter of the function. When a function is called, a value can be passed into the function. Then, when you’re inside the function, that data is in the batterup variable. Functions can be passed just about any data you want to use, including text strings, numbers, or even other JavaScript objects. For example, the batterup variable could be passed a player’s name as a text string ("Mantle"), or his number in the lineup (7) (although mixing the two can be a very bad idea unless you ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.