JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS Advanced
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window
JS Form
JS Browser

Examples/Quiz
JS Examples
JS Quiz Test

References
JS Objects

Resources
JS Books

JavaScript Math Object

Examples

The Math object

The built-in Math object includes mathematical constants and functions. You do not need to create the Math object before using it.

To store a random number between 0 and 1 in a variable called "r_number":

r_number=Math.random()

To store the rounded number of 8.6 in a variable called "r_number":

r_number=Math.round(8.6)


The Most Common Methods

NN: Netscape, IE: Internet Explorer, ECMA: Web Standard

Methods Explanation NN  IE ECMA
max(x,y) Returns the number with the highest value of x and y 2.0 3.0 1.0
min(x,y) Returns the number with the lowest value of x and y 2.0 3.0 1.0
random() Returns a random number between 0 and 1 2.0 3.0 1.0
round(x) Rounds x to the nearest integer 2.0 3.0 1.0

Round
How to round a specified number to the nearest whole number

<html>
<body>
<script type="text/javascript">
document.write(Math.round(7.25))
</script>
</body>
</html>

Random number
The random method returns a random number between 0 and 1

<html>
<body>
<script type="text/javascript">
document.write(Math.random())
</script>
</body>
</html>

Random number from 0-10
How to write a random number from 0 to 10, using the round and the random method.

<html>
<body>
<script type="text/javascript">
no=Math.random()*10
document.write(Math.floor(no))
</script>
</body>
</html>

Max number
How to test which of two numbers, has the highest value. 

<html>
<body>
<script type="text/javascript">
document.write(Math.max(2,4))
</script>
</body>
</html>

Min number
How to test which of two numbers, has the lowest value.

<html>
<body>
<script type="text/javascript">
document.write(Math.min(2,4))
</script>
</body>
</html>