JS: Math

By Xah Lee. Date: . Last updated: .

Math is the value of the property key "Math" of the global object. [JS: the Global Object]

console.log( window.Math === Math ); // true

Type

Type of Math is object.

[see JS: Value Types]

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

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

Parent

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

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

Purpose

Purpose of Math object is to provide a namespace for all math functions and math constants.

The Math object is a “special” object. It is similar in concept to Java's static class. You don't create Math object to use it. You use it directly, for its property as math constants, and its methods as math functions.

Properties

Value Properties

• Math.E
• Math.LN10
• Math.LN2
• Math.LOG10E
• Math.LOG2E
• Math.PI
• Math.SQRT1_2
• Math.SQRT2
• Math [ Symbol.toStringTag ] (ES2015)

Function Properties

common

1. Math.max
2. Math.min
3. Math.random
4. Math.sign (ES2015)
5. Math.abs

round

1. Math.ceil
2. Math.floor
3. Math.round
4. Math.fround (ES2015)
5. Math.trunc (ES2015)

power

1. Math.pow
2. Math.sqrt
3. Math.cbrt (ES2015)

exponential and logarithm

1. Math.exp
2. Math.expm1 (ES2015)
3. Math.log
4. Math.log1p (ES2015)
5. Math.log10 (ES2015)
6. Math.log2 (ES2015)

trig

1. Math.sin
2. Math.cos
3. Math.tan
4. Math.asin
5. Math.acos
6. Math.atan
7. Math.atan2
8. Math.hypot (ES2015)

hyperbolic trig

1. Math.sinh (ES2015)
2. Math.cosh (ES2015)
3. Math.tanh (ES2015)
4. Math.asinh (ES2015)
5. Math.acosh (ES2015)
6. Math.atanh (ES2015)

misc

1. Math.clz32 (ES2015)
2. Math.imul (ES2015)

Value Properties

Math.PI → π. Value is ≈3.1415

Math.E → Constant e. The number Limit[(1+1/n)^n,{n,∞}] . Value is ≈2.7182

Math.LN10 → 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_21/√2. Value is ≈0.7071

Math.SQRT2√2. Value is ≈1.4142

Math [ Symbol.toStringTag ] → value is the string "Math".

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

Methods

Common

Math.max(…) → Returns the largest of the arguments. [see JS: Get Max/Min Value of Array]

Math.min(…) → Returns the smallest of the arguments. [see JS: Get Max/Min Value of Array]

Math.random() → Return a real number between 0 to 1, including 0 but excluding 1.

Math.sign(x) → Returns 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) → Returns the nearest 32bits representation of x.

Math.trunc(x) → 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) → 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) → 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) → Base 10 log of x

Math.log1p(x) → Natural log of x+1

Math.log2(x) → Base 2 log of x

Trig Blue is sine curve. Pink is cosine. Red is tangent.

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) → Returns 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) → Returns the square root of a^2 + b^2 + c^2 …. This is useful to compute the length of a vector [a,b,c] in linear algebra.

All trig functions's argument are radians.

• To convert degree d to radians, do: d*Math.PI/180
• To convert degree r to degrees, do: r*180/Math.PI

Hyperbolic Functions

Math.sinh(x) → Hyperbolic sine. That is (e^x - e^(-x))/2

Math.cosh(x) → hyperbolic cosine. That is (e^x + e^(-x))/2

Math.tanh(x) → Hyperbolic tangent. That is Sinh[x]/Cosh[x]

Math.asinh(x) → Inverse hyperbolic sine.

Math.acosh(x) → Inverse hyperbolic cosine.

Math.atanh(x) → Inverse hyperbolic tangent.

Misc

Math.clz32(x) → Returns the number of leading zero bits in the 32-bit binary representation of x.

Math.imul(a, b) → multiplies a and b as though they were 32 bit signed integers.

JS Object Reference

Like it? Help me by telling your friends. Or, Put \$5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put \$5 at patreon and message me.