About Functions

Before you get into the next example, you need to learn a bit about functions, which you’ll use often when writing JavaScript. A function is a set of JavaScript statements that performs a task. Every function must be given a name (with one very rare exception, which we’ll discuss much later in this book) and can be invoked, or called, by other parts of the script.

Functions can be called as many times as needed during the running of the script. For example, let’s say that you’ve gotten some information that a user typed into a form, and you’ve saved it using JavaScript (there’s more about this sort of thing in Chapter 7, “Form Handling”). If you need to use that information again and again, you could repeat the same code over ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition 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.