JS References

JS Objects
JS HTML DOM

Suscriber with us



Receive HTML?

Syndicate

JS Math PDF Print E-mail

The Math object allows you to perform common mathematical tasks.

Math Object

The Math object allows you to perform common mathematical tasks.

The Math object includes several mathematical values and functions. You do not need to define the Math object before using it.

How to use round()?

<!--Created By phpguru.biz-->

document.write(Math.round(0.55) + "<br />");
document.write(Math.round(0.45) + "<br />");
document.write(Math.round(-5.35) + "<br />");
document.write(Math.round(-5.63));

<!--Created By phpguru.biz-->

How to use max() to return the highest value of two specified numbers?

<!--Created By phpguru.biz-->

document.write(Math.max(2,5) + "<br />");
document.write(Math.max(-8,3) + "<br />");
document.write(Math.max(6.78,6.89));

 <!--Created By phpguru.biz-->

Mathematical Values

JavaScript provides eight mathematical values (constants) that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.

You may reference these values from your JavaScript like this:

Math.E
Math.PI
Math.SQRT2

Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E

Math.LOG10E

Mathematical Methods

In addition to the mathematical values that can be accessed from the Math object there are also several functions (methods) available.

Examples of functions (methods):

The subsequent example uses the round() method of the Math object to round a number to the nearest integer:

document.write(Math.round(4.7));

The code above will result in the following output:

5

The following example uses the random() method of the Math object to return a random number between 0 and 1:

document.write(Math.random());

The code above can result in the following output:

0.3534388799698127

The subsequent example uses the floor() and random() methods of the Math object to return a random number between 0 and 10:

document.write(Math.floor(Math.random()*11));

The code above can result in the following output:

6

Examples

round()
How to use round().

random()
How to use random() to return a random number between 0 and 1.

max()
How to use max() to return the number with the highest value of two specified numbers.

min()
How to use min() to return the number with the lowest value of two specified numbers.





Digg!Del.icio.us!Google!Live!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Yahoo!Free social bookmarking plugins and extensions for Joomla! websites!
Last Updated ( Wednesday, 19 March 2008 )
 
< Prev   Next >

Polls

Which is the best Scripting language?