The `Math` object brings to JavaScript all the basic mathematical function-ality and constants you are likely to need.

Let's look at some examples of the `Math` object in action.

The π constant is one constant (equal to about 3.1415, although the numbers go on forever with the millionth number being a 1!) you might want to use, especially if you deal with circles. π returns the ratio of the circumference of a circle to its diameter. The following shows it in action as a simple circle area calculator using the mathematical formula `Pi x r^2` or `Pi x r x r` (see Figure 9.1):

<script language="JavaScript"> <!-- Cloaking device on! inpRadius = prompt("Enter the radius for your circle","Radius - digits only!"); alert(Math.PI ...

Start Free Trial

No credit card required