Math

Math 是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。

Math 适用于 Number 类型。它不支持 BigInt

描述

与其他全局对象不同的是,Math 不是一个构造器。 Math 的所有属性与方法都是静态的。引用圆周率的写法是 Math.PI,调用正余弦函数的写法是 Math.sin(x)x 是要传入的参数。Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。

属性

Math.E
欧拉常数,自然对数的底数, 约等于 2.718。
Math.LN2
2的自然对数, 约等于 0.693。
Math.LN10
10的自然对数, 约等于 2.303。
Math.LOG2E
以2为底E的对数, 约等于 1.443。
Math.LOG10E
以10为底E的对数, 约等于 0.434。
Math.PI
圆周率,一个圆的周长和直径之比,约等于 3.14159。
Math.SQRT1_2
1/2的平方根, 约等于 0.707。
Math.SQRT2
2的平方根,约等于 1.414。
Properties inherited from Object:

方法

需要注意的是,sin(), cos(), tan(), asin(), acos(), atan(), atan2() 是以弧度返回值的。可以通过除法(Math.PI / 180)或其他方法把弧度转换为角度。

需要注意的是,很多数学函数都有一个精度,并且精度在不同实现中也是不相同的。这就意味着不同的浏览器会给出不同的结果,甚至相同的 JS 引擎在不同的系统或架构下也会给出不同的结果。

Math.abs(x)
返回 x 的绝对值。
Math.acos(x)
返回 x 的反余弦值。
Math.acosh(x)
返回 x 的反双曲余弦值。
Math.asin(x)
返回 x 的反正弦值。
Math.asinh(x)
返回 x 的反双曲正弦值。
Math.atan(x)
以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
Math.atanh(x)
返回 x 的反双曲正切值。
Math.atan2(y, x)
返回 y/x 的反正切值。
Math.cbrt(x)
返回 x 的立方根。
Math.ceil(x)
返回 x 向上取整后的值。
Math.clz32(x)
返回一个 32 位整数的前导零的数量。
Math.cos(x)
返回 x 的余弦值。
Math.cosh(x)
返回 x 的双曲余弦值。
Math.exp(x)
返回 Ex,当x为参数,E 是欧拉常数(2.718...),自然对数的底数。
Math.expm1(x)
返回 exp(x) - 1 的值。
Math.floor(x)
返回小于 x 的最大整数。
Math.fround(x)
返回数字的最接近的单精度浮点型表示。
Math.hypot([x[, y[, …]]])
返回其参数平方和的平方根。
Math.imul(x, y)
返回 32 位整数乘法的结果。
Math.log(x)
返回一个数的自然对数(loge,即 ln)。
Math.log1p(x)
返回 1 加上一个数字的的自然对数(loge,即 ln)。
Math.log10(x)
返回以 10 为底数的 x 的对数。
Math.log2(x)
返回以 2 为底数的 x 的对数。
Math.max([x[, y[, …]]])
返回 0 个到多个数值中最大值。
Math.min([x[, y[, …]]])
返回 0 个到多个数值中最小值。
Math.pow(x, y)
返回 x 的 y 次幂。
Math.random()
返回 0 到 1 之间的伪随机数。
Math.round(x)
返回四舍五入后的整数。
Math.sign(x)
返回 x 的符号函数, 判定 x 是正数,负数还是 0。
Math.sin(x)
返回正弦值。
Math.sinh(x)
返回 x 的双曲正弦值。
Math.sqrt(x)
返回 x 的平方根。
Math.tan(x)
返回 x 的正切值。
Math.tanh(x)
返回 x 的双曲正切值。
Math.toSource()
返回字符串 "Math"
Math.trunc(x)
返回 x 的整数部分,直接去除小数点及之后的部分。

拓展 Math 对象

As with most of the built-in objects in JavaScript, the Math object can be extended with custom properties and methods. To extend the Math object, you do not use prototype. Instead, you directly extend Math:

Math.propName = propValue;
Math.methodName = methodRef;

For instance, the following example adds a method to the Math object for calculating the greatest common divisor of a list of arguments.

/* Variadic function -- Returns the greatest common divisor of a list of arguments */
Math.gcd = function() {
    if (arguments.length == 2) {
        if (arguments[1] == 0)
            return arguments[0];
        else
            return Math.gcd(arguments[1], arguments[0] % arguments[1]);
    } else if (arguments.length > 2) {
        var result = Math.gcd(arguments[0], arguments[1]);
        for (var i = 2; i < arguments.length; i++)
            result = Math.gcd(result, arguments[i]);
        return result;
    }
};

试试运行下面的代码:

console.log(Math.gcd(20, 30, 15, 70, 40)); // `5`

规范

规范 状态 说明
ECMAScript 1st Edition (ECMA-262) Standard 初始定义。在 JavaScript 1.1 中实现。
ECMAScript 5.1 (ECMA-262)
Math
Standard
ECMAScript 2015 (6th Edition, ECMA-262)
Math
Standard New methods log10(), log2(), log1p(), expm1(), cosh(), sinh(), tanh(), acosh(), asinh(), atanh(), hypot(), trunc(), sign(), imul(), fround(), cbrt() and clz32() added.
ECMAScript Latest Draft (ECMA-262)
Math
Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
EChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
LN2Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
LN10Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
LOG2EChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
LOG10EChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
PIChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
SQRT1_2Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
SQRT2Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
absChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
acosChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
acoshChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
asinChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
asinhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
atanChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
atan2Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
atanhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
cbrtChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
ceilChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
clz32Chrome Full support 38Edge Full support 12Firefox Full support 31IE No support NoOpera Full support 25Safari Full support YesWebView Android Full support YesChrome Android Full support 38Firefox Android Full support 31Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
cosChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
coshChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
expChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
expm1Chrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
floorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
froundChrome Full support 38Edge Full support 12Firefox Full support 26IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 26Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
hypotChrome Full support 38Edge Full support 12Firefox Full support 27IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 27Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
imulChrome Full support 28Edge Full support 12Firefox Full support 20IE No support NoOpera Full support 16Safari Full support 7WebView Android Full support YesChrome Android Full support 28Firefox Android Full support 20Opera Android Full support YesSafari iOS Full support 7Samsung Internet Android Full support Yesnodejs Full support 0.12
logChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
log1pChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
log2Chrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
log10Chrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
maxChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
minChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
powChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
randomChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
roundChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
signChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.12
sinChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
sinhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
sqrtChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
tanChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
tanhChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
truncChrome Full support 38Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support 38Firefox Android Full support 25Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

参见