# Math

## BaselineWidely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

`Math` 是一個擁有數學常數及數學函數（非函式物件）屬性及方法的內建物件。

## 屬性

`Math.E`

`Math.LN2`

2 的自然對數，約為 0.693。

`Math.LN10`

10 的自然對數，約為 2.303。

`Math.LOG2E`

`Math.LOG10E`

`Math.PI`

`Math.SQRT1_2`

1/2 的平方根；也就是 1 除以 2 的平方根，約為 0.707。

`Math.SQRT2`

2 的平方根，約為 1.414。

## 方法

`Math.abs(x)`

`Math.acos(x)`

`Math.acosh(x)`

Returns the hyperbolic arccosine of a number.

`Math.asin(x)`

`Math.asinh(x)`

Returns the hyperbolic arcsine of a number.

`Math.atan(x)`

`Math.atanh(x)`

Returns the hyperbolic arctangent of a number.

`Math.atan2(y, x)`

Returns the arctangent of the quotient of its arguments.

`Math.cbrt(x)`

`Math.ceil(x)`

`Math.clz32(x)`

Returns the number of leading zeroes of a 32-bit integer.

`Math.cos(x)`

`Math.cosh(x)`

Returns the hyperbolic cosine of a number.

`Math.exp(x)`

`Math.expm1(x)`

`Math.floor(x)`

`Math.fround(x)`

Returns the nearest single precision float representation of a number.

`Math.hypot([x[, y[, …]]])`

`Math.imul(x, y)`

Returns the result of a 32-bit integer multiplication.

`Math.log(x)`

`Math.log1p(x)`

`Math.log10(x)`

`Math.log2(x)`

`Math.max([x[, y[, …]]])`

`Math.min([x[, y[, …]]])`

`Math.pow(x, y)`

`Math.random()`

`Math.round(x)`

`Math.sign(x)`

`Math.sin(x)`

`Math.sinh(x)`

Returns the hyperbolic sine of a number.

`Math.sqrt(x)`

`Math.tan(x)`

`Math.tanh(x)`

Returns the hyperbolic tangent of a number.

`Math.toSource()` 非標準

`Math.trunc(x)`

Returns the integral part of the number x, removing any fractional digits.

## 擴充 `Math` 物件

As 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.

js
``````/* 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;
}
};
``````

Try it:

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

## 規範

Specification
ECMAScript Language Specification
# sec-math-object

## 瀏覽器相容性

BCD tables only load in the browser