JavaScript Math



The function of the Math (arithmetic) object is to perform common arithmetic tasks.



Online example

round()
How to use round().

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p id="demo">单击按钮舍入与“2.5”最接近的整数</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	document.getElementById("demo").innerHTML=Math.round(2.5);
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

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

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p id="demo">点击按钮显示一个随机数</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	document.getElementById("demo").innerHTML=Math.random();
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

max()
How to use max() to return the larger of two given numbers. (Prior to ECMASCript v3, this method had only two parameters.)

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p id="demo">单击按钮返回5到10之间的最大值。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	document.getElementById("demo").innerHTML=Math.max(5,10);
}
</script>

</body>
</html>

Run Instance»

Click "Run Example" button to view the online example

min()
How to use min() to return the smaller of two given numbers. (Prior to ECMASCript v3, this method had only two parameters.)

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p id="demo">单击按钮返回5到10之间最小的值。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	document.getElementById("demo").innerHTML=Math.min(5,10);
}
</script>

</body>
</html>

Run Instance»

Click "Run Example" button to view the online example


Complete Math Object Reference Manual

We provide a reference manual for JavaScript Math objects, which includes all properties available for arithmetic objects and methods.

This manual contains detailed descriptions and related examples of each property and method.


Math object

The function of the Math (arithmetic) object is to perform common arithmetic tasks.

The Math object provides a variety of arithmetic value types and functions. There is no need to define this object before using it.

Syntax for using Math’s properties/methods:

var x=Math.PI;
var y=Math.sqrt(16);

Note: The Math object does not need to be defined before using this object.


Arithmetic values

JavaScript provides 8 arithmetic values ​​that can be accessed by the Math object:

You can refer to the following methods of using Javascript constants:

Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E


Arithmetic methods

In addition to the arithmetic values ​​that can be accessed by the Math object, there are several functions (methods) that can be used.

The following example uses the round method of the Math object to round a number.

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

The output of the above code is:

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 output of the above code is:

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

document.write(Math.floor(Math.random()*11));
The output of the above code is:

9