The Math Object

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.

Using the PI Property

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 ...

Get JavaScript™ 1.5 by Example 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.