Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1082826 of Math

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Math
  • Revision title: Math
  • Revision id: 1082826
  • Created:
  • Creator: zhirzh
  • Is current revision? Yes
  • Comment

Revision Content

{{JSRef}}

Math is a built-in object that has properties and methods for mathematical constants and functions. Not a function object.

Description

Unlike the 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.

Properties

{{jsxref("Math.E")}}
Euler's constant and the base of natural logarithms, approximately 2.718.
{{jsxref("Math.LN2")}}
Natural logarithm of 2, approximately 0.693.
{{jsxref("Math.LN10")}}
Natural logarithm of 10, approximately 2.303.
{{jsxref("Math.LOG2E")}}
Base 2 logarithm of E, approximately 1.443.
{{jsxref("Math.LOG10E")}}
Base 10 logarithm of E, approximately 0.434.
{{jsxref("Math.PI")}}
Ratio of the circumference of a circle to its diameter, approximately 3.14159.
{{jsxref("Math.SQRT1_2")}}
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
{{jsxref("Math.SQRT2")}}
Square root of 2, approximately 1.414.

Methods

Note that the trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2()) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180), and multiply by this to convert the other way.

Note that a lot of the math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.

{{jsxref("Global_Objects/Math/abs", "Math.abs(x)")}}
Returns the absolute value of a number.
{{jsxref("Global_Objects/Math/acos", "Math.acos(x)")}}
Returns the arccosine of a number.
{{jsxref("Global_Objects/Math/acosh", "Math.acosh(x)")}}
Returns the hyperbolic arccosine of a number.
{{jsxref("Global_Objects/Math/asin", "Math.asin(x)")}}
Returns the arcsine of a number.
{{jsxref("Global_Objects/Math/asinh", "Math.asinh(x)")}}
Returns the hyperbolic arcsine of a number.
{{jsxref("Global_Objects/Math/atan", "Math.atan(x)")}}
Returns the arctangent of a number.
{{jsxref("Global_Objects/Math/atanh", "Math.atanh(x)")}}
Returns the hyperbolic arctangent of a number.
{{jsxref("Global_Objects/Math/atan2", "Math.atan2(y, x)")}}
Returns the arctangent of the quotient of its arguments.
{{jsxref("Global_Objects/Math/cbrt", "Math.cbrt(x)")}}
Returns the cube root of a number.
{{jsxref("Global_Objects/Math/ceil", "Math.ceil(x)")}}
Returns the smallest integer greater than or equal to a number.
{{jsxref("Global_Objects/Math/clz32", "Math.clz32(x)")}}
Returns the number of leading zeroes of a 32-bit integer.
{{jsxref("Global_Objects/Math/cos", "Math.cos(x)")}}
Returns the cosine of a number.
{{jsxref("Global_Objects/Math/cosh", "Math.cosh(x)")}}
Returns the hyperbolic cosine of a number.
{{jsxref("Global_Objects/Math/exp", "Math.exp(x)")}}
Returns Ex, where x is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm.
{{jsxref("Global_Objects/Math/expm1", "Math.expm1(x)")}}
Returns subtracting 1 from exp(x).
{{jsxref("Global_Objects/Math/floor", "Math.floor(x)")}}
Returns the largest integer less than or equal to a number.
{{jsxref("Global_Objects/Math/fround", "Math.fround(x)")}}
Returns the nearest single precision float representation of a number.
{{jsxref("Global_Objects/Math/hypot", "Math.hypot([x[, y[, …]]])")}}
Returns the square root of the sum of squares of its arguments.
{{jsxref("Global_Objects/Math/imul", "Math.imul(x, y)")}}
Returns the result of a 32-bit integer multiplication.
{{jsxref("Global_Objects/Math/log", "Math.log(x)")}}
Returns the natural logarithm (loge, also ln) of a number.
{{jsxref("Global_Objects/Math/log1p", "Math.log1p(x)")}}
Returns the natural logarithm (loge, also ln) of 1 + x for a number x.
{{jsxref("Global_Objects/Math/log10", "Math.log10(x)")}}
Returns the base 10 logarithm of a number.
{{jsxref("Global_Objects/Math/log2", "Math.log2(x)")}}
Returns the base 2 logarithm of a number.
{{jsxref("Global_Objects/Math/max", "Math.max([x[, y[, …]]])")}}
Returns the largest of zero or more numbers.
{{jsxref("Global_Objects/Math/min", "Math.min([x[, y[, …]]])")}}
Returns the smallest of zero or more numbers.
{{jsxref("Global_Objects/Math/pow", "Math.pow(x, y)")}}
Returns base to the exponent power, that is, baseexponent.
{{jsxref("Global_Objects/Math/random", "Math.random()")}}
Returns a pseudo-random number between 0 and 1.
{{jsxref("Global_Objects/Math/round", "Math.round(x)")}}
Returns the value of a number rounded to the nearest integer.
{{jsxref("Global_Objects/Math/sign", "Math.sign(x)")}}
Returns the sign of the x, indicating whether x is positive, negative or zero.
{{jsxref("Global_Objects/Math/sin", "Math.sin(x)")}}
Returns the sine of a number.
{{jsxref("Global_Objects/Math/sinh", "Math.sinh(x)")}}
Returns the hyperbolic sine of a number.
{{jsxref("Global_Objects/Math/sqrt", "Math.sqrt(x)")}}
Returns the positive square root of a number.
{{jsxref("Global_Objects/Math/tan", "Math.tan(x)")}}
Returns the tangent of a number.
{{jsxref("Global_Objects/Math/tanh", "Math.tanh(x)")}}
Returns the hyperbolic tangent of a number.
Math.toSource() {{non-standard_inline}}
Returns the string "Math".
{{jsxref("Global_Objects/Math/trunc", "Math.trunc(x)")}}
Returns the integral part of the number x, removing any fractional digits.

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.8', 'Math')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-math-object', 'Math')}} {{Spec2('ES6')}} New methods {{jsxref("Math.log10()", "log10()")}}, {{jsxref("Math.log2()", "log2()")}}, {{jsxref("Math.log1p()", "log1p()")}}, {{jsxref("Math.expm1()", "expm1()")}}, {{jsxref("Math.cosh()", "cosh()")}}, {{jsxref("Math.sinh()", "sinh()")}}, {{jsxref("Math.tanh()", "tanh()")}}, {{jsxref("Math.acosh()", "acosh()")}}, {{jsxref("Math.asinh()", "asinh()")}}, {{jsxref("Math.atanh()", "atanh()")}}, {{jsxref("Math.hypot()", "hypot()")}}, {{jsxref("Math.trunc()", "trunc()")}}, {{jsxref("Math.sign()", "sign()")}}, {{jsxref("Math.imul()", "imul()")}}, {{jsxref("Math.fround()", "fround()")}}, {{jsxref("Math.cbrt()", "cbrt()")}} and {{jsxref("Math.clz32()", "clz32()")}} added.
{{SpecName('ESDraft', '#sec-math-object', 'Math')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • {{jsxref("Number")}}

Revision Source

<div>{{JSRef}}</div>

<p><strong><code>Math</code></strong> is a built-in object that has properties and methods for mathematical constants and functions. Not a function object.</p>

<h2 id="Description">Description</h2>

<p>Unlike the other global objects, <code>Math</code> is not a constructor. All properties and methods of <code>Math</code> are static. You refer to the constant pi as <code>Math.PI</code> and you call the sine function as <code>Math.sin(x)</code>, where <code>x</code> is the method's argument. Constants are defined with the full precision of real numbers in JavaScript.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{jsxref("Math.E")}}</dt>
 <dd>Euler's constant and the base of natural logarithms, approximately 2.718.</dd>
 <dt>{{jsxref("Math.LN2")}}</dt>
 <dd>Natural logarithm of 2, approximately 0.693.</dd>
 <dt>{{jsxref("Math.LN10")}}</dt>
 <dd>Natural logarithm of 10, approximately 2.303.</dd>
 <dt>{{jsxref("Math.LOG2E")}}</dt>
 <dd>Base 2 logarithm of E, approximately 1.443.</dd>
 <dt>{{jsxref("Math.LOG10E")}}</dt>
 <dd>Base 10 logarithm of E, approximately 0.434.</dd>
 <dt>{{jsxref("Math.PI")}}</dt>
 <dd>Ratio of the circumference of a circle to its diameter, approximately 3.14159.</dd>
 <dt>{{jsxref("Math.SQRT1_2")}}</dt>
 <dd>Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.</dd>
 <dt>{{jsxref("Math.SQRT2")}}</dt>
 <dd>Square root of 2, approximately 1.414.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<div class="note">
<p>Note that the trigonometric functions (<code>sin()</code>, <code>cos()</code>, <code>tan()</code>, <code>asin()</code>, <code>acos()</code>, <code>atan()</code>, <code>atan2()</code>) expect or return angles in radians. To convert radians to degrees, divide by <code>(Math.PI / 180)</code>, and multiply by this to convert the other way.</p>
</div>

<div class="note">
<p>Note that a lot of the math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.</p>
</div>

<dl>
 <dt>{{jsxref("Global_Objects/Math/abs", "Math.abs(x)")}}</dt>
 <dd>Returns the absolute value of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/acos", "Math.acos(x)")}}</dt>
 <dd>Returns the arccosine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/acosh", "Math.acosh(x)")}}</dt>
 <dd>Returns the hyperbolic arccosine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/asin", "Math.asin(x)")}}</dt>
 <dd>Returns the arcsine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/asinh", "Math.asinh(x)")}}</dt>
 <dd>Returns the hyperbolic arcsine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/atan", "Math.atan(x)")}}</dt>
 <dd>Returns the arctangent of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/atanh", "Math.atanh(x)")}}</dt>
 <dd>Returns the hyperbolic arctangent of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/atan2", "Math.atan2(y, x)")}}</dt>
 <dd>Returns the arctangent of the quotient of its arguments.</dd>
 <dt>{{jsxref("Global_Objects/Math/cbrt", "Math.cbrt(x)")}}</dt>
 <dd>Returns the cube root of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/ceil", "Math.ceil(x)")}}</dt>
 <dd>Returns the smallest integer greater than or equal to a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/clz32", "Math.clz32(x)")}}</dt>
 <dd>Returns the number of leading zeroes of a 32-bit integer.</dd>
 <dt>{{jsxref("Global_Objects/Math/cos", "Math.cos(x)")}}</dt>
 <dd>Returns the cosine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/cosh", "Math.cosh(x)")}}</dt>
 <dd>Returns the hyperbolic cosine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/exp", "Math.exp(x)")}}</dt>
 <dd>Returns E<sup>x</sup>, where <var>x</var> is the argument, and E is Euler's constant (2.718…), the base of the natural logarithm.</dd>
 <dt>{{jsxref("Global_Objects/Math/expm1", "Math.expm1(x)")}}</dt>
 <dd>Returns subtracting 1 from <code>exp(x)</code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/floor", "Math.floor(x)")}}</dt>
 <dd>Returns the largest integer less than or equal to a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/fround", "Math.fround(x)")}}</dt>
 <dd>Returns the nearest <a href="https://en.wikipedia.org/wiki/Single-precision_floating-point_format" title="link to the wikipedia page on single precision">single precision</a> float representation of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/hypot", "Math.hypot([x[, y[, …]]])")}}</dt>
 <dd>Returns the square root of the sum of squares of its arguments.</dd>
 <dt>{{jsxref("Global_Objects/Math/imul", "Math.imul(x, y)")}}</dt>
 <dd>Returns the result of a 32-bit integer multiplication.</dd>
 <dt>{{jsxref("Global_Objects/Math/log", "Math.log(x)")}}</dt>
 <dd>Returns the natural logarithm (log<sub>e</sub>, also ln) of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/log1p", "Math.log1p(x)")}}</dt>
 <dd>Returns the natural logarithm (log<sub>e</sub>, also ln) of <code>1 + x</code> for a number x.</dd>
 <dt>{{jsxref("Global_Objects/Math/log10", "Math.log10(x)")}}</dt>
 <dd>Returns the base 10 logarithm of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/log2", "Math.log2(x)")}}</dt>
 <dd>Returns the base 2 logarithm of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/max", "Math.max([x[, y[, …]]])")}}</dt>
 <dd>Returns the largest of zero or more numbers.</dd>
 <dt>{{jsxref("Global_Objects/Math/min", "Math.min([x[, y[, …]]])")}}</dt>
 <dd>Returns the smallest of zero or more numbers.</dd>
 <dt>{{jsxref("Global_Objects/Math/pow", "Math.pow(x, y)")}}</dt>
 <dd>Returns base to the exponent power, that is, <code>base<sup>exponent</sup></code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/random", "Math.random()")}}</dt>
 <dd>Returns a pseudo-random number between 0 and 1.</dd>
 <dt>{{jsxref("Global_Objects/Math/round", "Math.round(x)")}}</dt>
 <dd>Returns the value of a number rounded to the nearest integer.</dd>
 <dt>{{jsxref("Global_Objects/Math/sign", "Math.sign(x)")}}</dt>
 <dd>Returns the sign of the x, indicating whether x is positive, negative or zero.</dd>
 <dt>{{jsxref("Global_Objects/Math/sin", "Math.sin(x)")}}</dt>
 <dd>Returns the sine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/sinh", "Math.sinh(x)")}}</dt>
 <dd>Returns the hyperbolic sine of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/sqrt", "Math.sqrt(x)")}}</dt>
 <dd>Returns the positive square root of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/tan", "Math.tan(x)")}}</dt>
 <dd>Returns the tangent of a number.</dd>
 <dt>{{jsxref("Global_Objects/Math/tanh", "Math.tanh(x)")}}</dt>
 <dd>Returns the hyperbolic tangent of a number.</dd>
 <dt><code>Math.toSource()</code> {{non-standard_inline}}</dt>
 <dd>Returns the string <code>"Math"</code>.</dd>
 <dt>{{jsxref("Global_Objects/Math/trunc", "Math.trunc(x)")}}</dt>
 <dd>Returns the integral part of the number x, removing any fractional digits.</dd>
</dl>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.8', 'Math')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math-object', 'Math')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>New methods {{jsxref("Math.log10()", "log10()")}}, {{jsxref("Math.log2()", "log2()")}}, {{jsxref("Math.log1p()", "log1p()")}}, {{jsxref("Math.expm1()", "expm1()")}}, {{jsxref("Math.cosh()", "cosh()")}}, {{jsxref("Math.sinh()", "sinh()")}}, {{jsxref("Math.tanh()", "tanh()")}}, {{jsxref("Math.acosh()", "acosh()")}}, {{jsxref("Math.asinh()", "asinh()")}}, {{jsxref("Math.atanh()", "atanh()")}}, {{jsxref("Math.hypot()", "hypot()")}}, {{jsxref("Math.trunc()", "trunc()")}}, {{jsxref("Math.sign()", "sign()")}}, {{jsxref("Math.imul()", "imul()")}}, {{jsxref("Math.fround()", "fround()")}}, {{jsxref("Math.cbrt()", "cbrt()")}} and {{jsxref("Math.clz32()", "clz32()")}} added.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math-object', 'Math')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Number")}}</li>
</ul>
Revert to this revision