Math
is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object.
Math
works with the
Number
type. It doesn't work with
BigInt
.
Unlike many other global objects,
Math
is not a constructor. All properties and methods of
Math
are static. You refer to the constant pi as
Math.PI
and you call the sine function as
Math.sin(
x
)
, where
x
is the method’s argument. Constants are defined with the full precision of real numbers in JavaScript.
注意:
Many
Math
functions have a precision that’s
implementation-dependent.
This means that different browsers can give a different result. Even the same JavaScript engine on a different OS or architecture can give different results!
Math.E
2.718
.
Math.LN2
2
; approximately
0.693
.
Math.LN10
10
; approximately
2.303
.
Math.LOG2E
E
; approximately
1.443
.
Math.LOG10E
E
; approximately
0.434
.
Math.PI
3.14159
.
Math.SQRT1_2
0.707
.
Math.SQRT2
2
; approximately
1.414
.
Math.abs(
x
)
x
.
Math.acos(
x
)
x
.
Math.acosh(
x
)
x
.
Math.asin(
x
)
x
.
Math.asinh(
x
)
Returns the hyperbolic arcsine of a number.
Math.atan(
x
)
x
.
Math.atanh(
x
)
x
.
Math.atan2(
y
,
x
)
Returns the arctangent of the quotient of its arguments.
Math.cbrt(
x
)
x
.
Math.ceil(
x
)
x
.
Math.clz32(
x
)
x
.
Math.cos(
x
)
x
.
Math.cosh(
x
)
x
.
Math.exp(
x
)
E
x
, where
x
is the argument, and
E
is Euler's constant (
2.718
…, the base of the natural logarithm).
Math.expm1(
x
)
1
from
exp(
x
)
.
Math.floor(
x
)
x
.
Math.fround(
x
)
x
.
Math.hypot([
x
[,
y
[, …]]])
Returns the square root of the sum of squares of its arguments.
Math.imul(
x
,
y
)
x
and
y
.
Math.log(
x
)
x
.
Math.log1p(
x
)
1 +
x
for the number
x
.
Math.log10(
x
)
x
.
Math.log2(
x
)
x
.
Math.max([
x
[,
y
[, …]]])
Returns the largest of zero or more numbers.
Math.min([
x
[,
y
[, …]]])
Returns the smallest of zero or more numbers.
Math.pow(
x
,
y
)
x
to the exponent power
y
(that is,
x
y
).
Math.random()
0
and
1
.
Math.round(
x
)
x
rounded to the nearest integer.
Math.sign(
x
)
x
, indicating whether
x
is positive, negative, or zero.
Math.sin(
x
)
x
.
Math.sinh(
x
)
x
.
Math.sqrt(
x
)
x
.
Math.tan(
x
)
x
.
Math.tanh(
x
)
x
.
Math.trunc(
x
)
x
, removing any fractional digits.
The trigonometric functions
sin()
,
cos()
,
tan()
,
asin()
,
acos()
,
atan()
,和
atan2()
expect (and return) angles in
radians
.
Since humans tend to think in degrees, and some functions (such as CSS transforms) can accept degrees, it is a good idea to keep functions handy that convert between the two:
function degToRad(degrees) {
return degrees * (Math.PI / 180);
};
function radToDeg(rad) {
return rad / (Math.PI / 180);
};
If we want to calculate the height of an equalateral triangle, and we know its side length is 100, we can use the formulae length of the adjacent multiplied by the tangent of the angle is equal to the opposite.
In JavaScript, we can do this with the following:
50 * Math.tan(degToRad(60)).
We use our
degToRad()
function to convert 60 degrees to radians, as
Math.tan()
expects an input value in radians.
This can be achieved with a combination of
Math.random()
and
Math.floor()
:
function random(min, max) {
const num = Math.floor(Math.random() * (max - min + 1)) + min;
return num;
}
random(1, 10);
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Math' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
E
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
LN2
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
LN10
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
LOG2E
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
LOG10E
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
PI
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
SQRT1_2
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
SQRT2
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
abs
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
acos
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
acosh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
asin
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
asinh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
atan
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
atan2
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
atanh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
cbrt
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
ceil
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
clz32
|
Chrome 38 | Edge 12 | Firefox 31 | IE No | Opera 25 | Safari 7 | WebView Android 38 | Chrome Android 38 | Firefox Android 31 | Opera Android 25 | Safari iOS 7 | Samsung Internet Android 3.0 | nodejs 0.12 |
cos
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
cosh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
exp
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
expm1
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
floor
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
fround
|
Chrome 38 | Edge 12 | Firefox 26 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 26 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
hypot
|
Chrome 38 | Edge 12 | Firefox 27 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 27 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
imul
|
Chrome 28 | Edge 12 | Firefox 20 | IE No | Opera 16 | Safari 7 | WebView Android ≤37 | Chrome Android 28 | Firefox Android 20 | Opera Android 15 | Safari iOS 7 | Samsung Internet Android 1.5 | nodejs 0.12 |
log
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
log1p
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
log2
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
log10
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
max
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
min
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
pow
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
random
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
round
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
sign
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 9 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
sin
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
sinh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
sqrt
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
tan
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
tanh
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
trunc
|
Chrome 38 | Edge 12 | Firefox 25 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 25 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
Math
Math.abs()
Math.acos()
Math.acosh()
Math.asin()
Math.asinh()
Math.atan()
Math.atan2()
Math.atanh()
Math.cbrt()
Math.ceil()
Math.clz32()
Math.cos()
Math.cosh()
Math.exp()
Math.expm1()
Math.floor()
Math.fround()
Math.hypot()
Math.imul()
Math.log()
Math.log10()
Math.log1p()
Math.log2()
Math.max()
Math.min()
Math.pow()
Math.random()
Math.round()
Math.sign()
Math.sin()
Math.sinh()
Math.sqrt()
Math.tan()
Math.tanh()
Math.trunc()
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()