Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Math

BaselineWidely available *

TheMath namespace object contains static properties and methods for mathematical constants and functions.

Math works with theNumber type. It doesn't work withBigInt.

Description

Unlike most global objects,Math is not a constructor. You cannot use it with thenew operator or invoke theMath object as a function. All properties and methods ofMath are static.

Note:ManyMath functions have a precision that'simplementation-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!

Static properties

Math.E

Euler's number and the base of natural logarithms; approximately2.718.

Math.LN10

Natural logarithm of10; approximately2.303.

Math.LN2

Natural logarithm of2; approximately0.693.

Math.LOG10E

Base-10 logarithm ofE; approximately0.434.

Math.LOG2E

Base-2 logarithm ofE; approximately1.443.

Math.PI

Ratio of a circle's circumference to its diameter; approximately3.14159.

Math.SQRT1_2

Square root of ½; approximately0.707.

Math.SQRT2

Square root of2; approximately1.414.

Math[Symbol.toStringTag]

The initial value of the[Symbol.toStringTag] property is the string"Math". This property is used inObject.prototype.toString().

Static methods

Math.abs()

Returns the absolute value of the input.

Math.acos()

Returns the arccosine of the input.

Math.acosh()

Returns the hyperbolic arccosine of the input.

Math.asin()

Returns the arcsine of the input.

Math.asinh()

Returns the hyperbolic arcsine of a number.

Math.atan()

Returns the arctangent of the input.

Math.atan2()

Returns the arctangent of the quotient of its arguments.

Math.atanh()

Returns the hyperbolic arctangent of the input.

Math.cbrt()

Returns the cube root of the input.

Math.ceil()

Returns the smallest integer greater than or equal to the input.

Math.clz32()

Returns the number of leading zero bits of the 32-bit integer input.

Math.cos()

Returns the cosine of the input.

Math.cosh()

Returns the hyperbolic cosine of the input.

Math.exp()

Returns ex, where x is the argument, and e is Euler's number (2.718…, the base of the natural logarithm).

Math.expm1()

Returns subtracting1 fromexp(x).

Math.floor()

Returns the largest integer less than or equal to the input.

Math.f16round()

Returns the nearesthalf precision float representation of the input.

Math.fround()

Returns the nearestsingle precision float representation of the input.

Math.hypot()

Returns the square root of the sum of squares of its arguments.

Math.imul()

Returns the result of the 32-bit integer multiplication of the inputs.

Math.log()

Returns the natural logarithm (㏒e; also, ㏑) of the input.

Math.log10()

Returns the base-10 logarithm of the input.

Math.log1p()

Returns the natural logarithm (㏒e; also ㏑) of1 + x for the numberx.

Math.log2()

Returns the base-2 logarithm of the input.

Math.max()

Returns the largest of zero or more numbers.

Math.min()

Returns the smallest of zero or more numbers.

Math.pow()

Returns basex to the exponent powery (that is,xy).

Math.random()

Returns a pseudo-random number between0 and1.

Math.round()

Returns the value of the input rounded to the nearest integer.

Math.sign()

Returns the sign of the input, indicating whether it is positive, negative, or zero.

Math.sin()

Returns the sine of the input.

Math.sinh()

Returns the hyperbolic sine of the input.

Math.sqrt()

Returns the positive square root of the input.

Math.sumPrecise()

Returns the sum of a passed iterable of numbers, avoiding floating point precision loss in intermediate results.

Math.tan()

Returns the tangent of the input.

Math.tanh()

Returns the hyperbolic tangent of the input.

Math.trunc()

Returns the integer portion of the input, removing any fractional digits.

Examples

Converting between degrees and radians

The trigonometric functionssin(),cos(),tan(),asin(),acos(),atan(), andatan2() expect (and return) angles inradians.

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:

js
function degToRad(degrees) {  return degrees * (Math.PI / 180);}function radToDeg(rad) {  return rad / (Math.PI / 180);}

Calculating the height of an equilateral triangle

If we want to calculate the height of an equilateral triangle, and we know its side length is 100, we can use the formulaelength of the adjacent multiplied by the tangent of the angle is equal to the opposite.

An equilateral triangle where a perpendicular of one edge is drawn from the opposite vertex, forming a right triangle with three sides marked as "adjacent", "opposite", and "hypotenuse". The angle between the "adjacent" and "hypotenuse" sides is 60 degrees.

In JavaScript, we can do this with the following:

js
50 * Math.tan(degToRad(60));

We use ourdegToRad() function to convert 60 degrees to radians, asMath.tan() expects an input value in radians.

Returning a random integer between two bounds

This can be achieved with a combination ofMath.random() andMath.floor():

js
function random(min, max) {  const num = Math.floor(Math.random() * (max - min + 1)) + min;  return num;}random(1, 10);

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-math-object

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp