The Math object is a predefined JavaScript object used to perform mathematical operations. It's also used to obtain the values of predefined mathematical constants, i.e., Pi (π) or the square root of a number. In addition, it can also be used to generate random numbers.
It's important to remember that the Math object begins with an uppercase "M." If a lower case "m" is used, the JavaScript interpreter will not recognize it as an object.
(Note: We won't cover all of these properties and methods in detail as some of them are a little more complicated than what we are dealing with right now.)
The ratio of the circumference of a circle to its diameter, approximately 3.14159
SQRT1_2
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707
SQRT2
Square root of 2, approximately 1.414
Using Pi
You can use this script to calculate the circumference of a circle using Pi by entering the radius:
Let's see what we did in this script.
First, we created a function called circum.
Then, we declared the variable radius and gave it the value of the results of a prompt box, which obtains the size of the radius from the user.
Next, we declared the variable circle and gave it the formula which will calculate the actual circumference of the circle, based on the information given to the radius variable.
Finally, an alert box displays the results of the calculation.
You would then add this in the body section of the page in order to call the script above:
Let's take a look at the JavaScript math methods and see what we can do with them.
Add htmlgoodies.com to your favorites Add htmlgoodies.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed