The Math ObjectHelpful methods for calculationsHome/JavaScript/Advanced/The Math Object
The Math object of JavaScript allows you to perform certain calculations by using method functions of the Math object. Also, it provides a few constants such as pi. The easiest way to get a value is to define a variable and set its value to a property or function of the Math Object. If you want to use a property, which returns a constant value (such as pi), you would write something like this: var my_car=Math.property; You would replace the word "property" with the property you want to use. So, if you want to use the value of pi in a variable, you would use the PI property of the Math object. You would write: var my_pie=Math.PI; This returns the pi constant, a number with lots of decimals, or 3.14..... If you want to use a member function, which performs a calculation, you would write something like this: var my_house=Math.function(x); Most of the member functions have one or more parameters, which is what the "x" is for. You can replace x with a number or variable. You would replace the word "function" with the function you want to use. For instance, if you want the square root of a number, you can call the square root member function of the Math object with the number as the parameter: var my_money=Math.sqrt(2); This gives back the square root of 2, which is another number with lots of decimals, around 1.41..... Now that you have seen how little money I have, be sure to take notice that the "M" in Math is always capitalized. The word Math is followed by the dot operator (.), and then the property or member function you want to use. These are not too difficult to use now that you know how to get to them. Here are some of the properties you can use, these return the constant values:
Below is a table of some of the commonly used member functions of the Math object. These functions calculate values based on the parameters you send to them (except the random function):
Hopefully, you will find some of these useful in your scripts. We may start using some of these in future tutorials, so be on the lookout! Well, let's move on to the next section: Using the Math.random() Function.
Other Topics: ASP/PHP | DHTML | Java The tutorials and articles on these pages are © 1997-2010 by John Pollock and may not be reposted without written permission from the author, and may not be reprinted for profit. Disclaimer. |
|
By: John Pollock |
|