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]

// type of Math
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]

// parent of Math
console.log ( Object.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

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

[see JS: 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 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.

[see JS: 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) → 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) → Computes 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) → Computes e ** x.

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

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

For trig functions review, see: Sine Curve

Hyperbolic Functions

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

Math.cosh(x) → hyperbolic cosine. (e^x + e^(-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

JS Object Reference

  1. Global
  2. Object
  3. Array
  4. Function
  5. String
  6. RegExp
  7. Date
  8. Set
  9. Map
  10. JSON
  11. Math
  12. Reflect
  13. Number
  14. Boolean
  15. Symbol
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Patreon me $5. Ask me question on patreon