__WHAT YOU WILL LEARN IN THIS CHAPTER: __

- Creating your own functions
- Identifying, creating, and using global and local variables
- Using functions as a value

**WROX.COM CODE DOWNLOADS FOR THIS CHAPTER**

You can find the wrox.com code downloads for this chapter at `http://www.wiley.com/go/BeginningJavaScript5E`

on the Download Code tab. You can also view all of the examples and related files at `http://beginningjs.com`

.

A function is something that performs a particular task. Take a pocket calculator as an example. It performs lots of basic calculations, such as addition and subtraction. However, many also have function keys that perform more complex operations. For example, some calculators have a button for calculating the square root of a number, and others even provide statistical functions, such as the calculation of an average. Most of these functions could be done with the basic mathematical operations of add, subtract, multiply, and divide, but that might take a lot of steps—it’s much simpler for the user if she only needs to press one button. All she needs to do is provide the data—numbers in this case—and the function key does the rest.

Functions in JavaScript work a little like the function buttons on a pocket calculator: They encapsulate a block of code that performs a certain task. Over the course of the book so far, you have come across a number of handy built-in functions that perform a certain task, such as the `parseInt()`

and `parseFloat() ...`

Start Free Trial

No credit card required