Java Reference
In-Depth Information
Functions and Scope
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 topic so far,
you have come across a number of handy built‐in functions that perform a certain task, such
as the parseInt() and parseFloat() functions, which convert strings to numbers, and the
isNaN() function, which tells you whether a particular value can be converted to a number.
Some of these functions return data, such as parseInt() , which returns an integer number;
 
Search WWH ::




Custom Search