PageResource.com - Web Development Tutorials

The Math Object

Helpful methods for calculations

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:

Math.PI Returns the constant pi
Math.SQRT2 Returns the square root of 2
Math.SQRT1_2 Returns the square root of 1/2

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):

Method Function What it Does
abs(x) Returns the absolute value of the variable x.
cos(x) Returns the cosine of the variable x.
log(x) Returns the natural log of the variable x.
max(x,z) Returns the larger of the two variables x and z.
min(x,z) Returns the smaller of the two variables x and z.
pow(x,z) Returns the value of the variable x to the zth power.
random() Returns a random number between 0 and 1.
round(x) Returns the variable x rounded to the nearest integer.
sin(x) Returns the sine of the variable x.
sqrt(x) Returns the square root of the variable x.
tan(x) Returns the tangent of the variable x.

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.

previous
Previous
By: John Pollock next
Next

Copyright © The Web Design Resource. All rights reserved. | Contact Us | Privacy Policy