JavaScript: Math Object

By Xah Lee. Date: . Last updated: .

Math is the value of the property key "Math" of the global object.

console.log( window.hasOwnProperty ( "Math" ) ) // true

Type of Math is object.

〔►see JavaScript: Data Types

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

The parent of Math is Object.prototype. 〔►see JavaScript: Prototype and Inheritance

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

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

Function Properties

common

  1. Math.max ( value1 , value2 , …values )
  2. Math.min ( value1 , value2 , …values )
  3. Math.random ( )
  4. Math.sign( x ) (ES2015)
  5. Math.abs ( x )

round

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

power

  1. Math.pow ( x, y )
  2. Math.sqrt ( x )
  3. Math.cbrt ( x ) (ES2015)

exponential and logarithm

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

trig

  1. Math.sin ( x )
  2. Math.cos ( x )
  3. Math.tan ( x )
  4. Math.asin ( x )
  5. Math.acos ( x )
  6. Math.atan ( x )
  7. Math.atan2 ( y, x )
  8. Math.hypot ( value1 , value2 , …values ) (ES2015)

hyperbolic trig

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

misc

  1. Math.clz32 ( x ) (ES2015)
  2. Math.imul ( x, y ) (ES2015)

Value Properties

Math.PI → π. Value is 3.1415…

Math.E → constant ℯ. Base of the natural logarithm. 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 ℯ. Value is 0.4342…

Math.LOG2E → base-2 log of ℯ. Value is 1.4426…

Math.SQRT1_21/√2. Value is 0.7071…

Math.SQRT2√2. Value is 1.4142…

Math.prototype [ Symbol.toStringTag ]

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

〔►see JavaScript: Determine Type of Object

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

ECMAScript 2015 §Numbers and Dates#sec-math-@@tostringtag

Methods

Common

Math.max(…) → Returns the largest of the arguments. 〔►see JavaScript: How to Get Max/Min Value of a Array?

Math.min(…) → Returns the smallest of the arguments. 〔►see JavaScript: How to Get Max/Min Value of a Array?

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

〔►see JavaScript: Random Range Function

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

ECMAScript 2015 §Numbers and Dates#sec-math.sign

Math.abs(x) → Absolute value.

Ceiling, Floor, Round

Math.ceil(x) → Returns a integer that's equal or greater than x.

Math.floor(x) → Returns a integer that's equal or less than x.

Math.round(x) → Returns 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) → Computes x^y.

Math.sqrt(x) → Square root.

Math.cbrt(x) → Returns cube root of x.

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

Exponential and Logarithm

Math.exp(x) → Computes ℯ^x.

Math.expm1(x) → Returns ℯ^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

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

Math.sin(x) → Sine function. Result is in range −π/2 to π/2.

Math.cos(x) → Cosine function. Result is in range −π/2 to π/2.

Math.tan(x) → Tangent function. Result is in range −π/2 to π/2.

Math.asin(x) → Inverse of sine function. Result is in range −π/2 to π/2.

Math.acos(x) → Inverse of cosine function. Result is in range 0 to π.

Math.atan(x) → Inverse of cosine function. Result is in range −π/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 …) → Returns Sqrt[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.

For trig functions review, see: Sine Curve

Hyperbolic Functions

Math.sinh(x) → Hyperbolic sine. (ℯ^x - ℯ^(-x))/2

Math.cosh(x) → hyperbolic cosine. (ℯ^x + ℯ^(-x))/2 〔►see Catenary

Math.tanh(x) → Hyperbolic tangent. 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.

Reference

ECMAScript 2015 §Numbers and Dates#sec-math-object

Object Reference

  1. JavaScript: Object Object
  2. JavaScript: Array Object
  3. JavaScript: Function Object
  4. JavaScript: String Object
  5. JavaScript: RegExp.prototype
  6. JavaScript: Date Object
  7. JavaScript: Set Object
  8. JavaScript: Map Object
  9. JavaScript: JSON Object
  10. JavaScript: Math Object
  11. JavaScript: Reflect Object
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.