JavaScript: Math

By Xah Lee. Date: . Last updated: .

Math is the value of the property key "Math" of the Global Object .

console.log( window["Math"] === Math ); // true

Type

Type of Math is Object .

console.log( typeof Math === "object" ); // true

console.log( Object.prototype.toString.call( Math ) === "[object Math]" ) // true

Parent

Parent of Math is Object.prototype. [see Prototype and Inheritance]

console.log( Reflect.getPrototypeOf ( Math ) === Object.prototype ); // true

Purpose

Purpose of Math object is as a namespace for math related functions and constants.

Properties

Constants

Math.PI
π. Value is ≈3.1415
Math.E
Constant e. The number Limit[(1+1/n)^n,{n,∞}] . Value is ≈2.7182
Math.LN10
(JS2015) natural log of 10. Value is ≈2.3025
Math.LN2
natural log of 2. Value is ≈0.6931
Math.LOG10E
base-10 log of e. Value is ≈0.4342
Math.LOG2E
base-2 log of e. Value is ≈1.4426
Math.SQRT1_2
1/(√2). Value is ≈0.7071
Math.SQRT2
√2. Value is ≈1.4142
Math [ Symbol.toStringTag ]
(JS2015) value is the string "Math".

Common Math Functions

Math.max(args)
Return the largest of the arguments. Example: Math.max(...myArray)
Math.min(args)
Return the smallest of the arguments. Example: Math.min(...myArray)
Math.random()
Return a real number between 0 to 1, including 0 but excluding 1. [see Random Range Function]
Math.sign(x)
Return 1 if x is positive, -1 if negative, 0 if positive 0, -0 if negative 0. (JavaScript's 0 can are signed. -0 === 0 eval to true.)
Math.abs(x)
Absolute value.

Ceiling, Floor, Round

Math.ceil(x)
Return a integer that's equal or greater than x.
Math.floor(x)
Return a integer that's equal or less than x.
Math.round(x)
Return a number to the nearest integer. 0.5 rounds up to 1.
Math.fround(x)
(JS2015) Returns the nearest 32bits representation of x.
Math.trunc(x)
(JS2015) Returns the integral part of x.

Power and Roots

Math.pow(x, y)
x raised to the power of y. Same as x ** y.
Math.sqrt(x)
Square root.
Math.cbrt(x)
(JS2015) Cube root.

To compute nth root, use Math.pow(x, 1/n).

Exponential and Logarithm

Math.exp(x)
e raised to the power of x. Same as e ** x.
Math.expm1(x)
(JS2015) Same as e ** x -1. The result is computed in a way that is accurate even when the value of x is close 0.
console.log( Math.expm1(0.01)  );
// 0.010050167084168058

console.log( Math.pow ( Math.E, 0.01 ) - 1 );
// 0.010050167084167949
Math.log(x)
Natural log of x
Math.log10(x)
(JS2015) Base 10 log of x
Math.log1p(x)
(JS2015) Natural log of x+1
Math.log2(x)
(JS2015) Base 2 log of x

Trig

Math.sin(x)
Sine function.
Math.cos(x)
Cosine function.
Math.tan(x)
Tangent function.
Math.asin(x)
Inverse of sine. Domain is -1 to 1. Range is 0 to π.
Math.acos(x)
Inverse of cosine. Domain is -1 to 1. Range is 0 to π.
Math.atan(x)
Inverse of tangent. No restriction on domain. Range is -π/2 to π/2.
Math.atan2(y, x)
Return the arc tangent of the quotient y/x, where the signs of y and x are used to determine the quadrant of the result. Result is in range from −π to +π.
Math.hypot(a, b, c etc)
(JS2015) Returns the square root of (a^2 + b^2 + c^2 etc). This is useful to compute the length of a higher dimension vector in linear algebra.

All trig functions's argument are radians.

Hyperbolic Functions

Math.sinh(x)
(JS2015) Hyperbolic sine. That is (e^x - e^(-x))/2
Math.cosh(x)
(JS2015) hyperbolic cosine. That is (e^x + e^(-x))/2
Math.tanh(x)
(JS2015) Hyperbolic tangent. That is Sinh[x]/Cosh[x]
Math.asinh(x)
(JS2015) Inverse hyperbolic sine.
Math.acosh(x)
(JS2015) Inverse hyperbolic cosine.
Math.atanh(x)
(JS2015) Inverse hyperbolic tangent.

Misc

Math.clz32(x)
(JS2015) Returns the number of leading zero bits in the 32-bit binary representation of x.
Math.imul(a, b)
(JS2015) multiplies a and b as though they were 32 bit signed integers.
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Math