Open Directory Site JavaScript Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> JavaScript >> JavaScript Math Object

 JavaScript Math Object

 

The Math object

A built-in object that let you perform mathematic functions. This object is a bit different from the other objects in JavaScript. For example, the Math object is not created with the "new" operator, if you do so, an error will occur. This is how you create the object:

Math.random()

The most common methods

Methods Explanation NN IE ECMA

max() Returns the number with the highest value of two numbers 2.0 3.0 1.0

min() Returns the number with the lowest value of two numbers 2.0 3.0 1.0

random() Returns a random number 2.0 3.0 1.0

round() Returns a number rounded to the nearest whole number 2.0 3.0 1.0

Examples

1. Round

How to round a specified number to the nearest whole number

Coding

<html>

<body>

<script language="JavaScript">

document.write(Math.round(7.25))

</script>

</body>

</html>

Output

7

2. Random Number

How to write a random number

Coding

<html>

<body>

<script language="JavaScript">

document.write(Math.random())

</script>

</body>

</html>

Output

0.3891641494127387

3. Max Number

How to test which number has the highest value.

Coding

<html>

<body>

<script language="JavaScript">

document.write(Math.max(2,4))

</script>

</body>

</html>

Output

4

4. Min Number

How to test which number has the lowest value.

Coding

<html>

<body>

<script language="JavaScript">

document.write(Math.min(2,4))

</script>

</body>

</html>

Output

2

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting