Please support this book: buy it or donate

16. Math



Math is an object with data properties and methods for processing numbers.

You can see it as a poor man’s module. Today, it would probably be created as a module, but it has existed since long before modules.

16.1. Data properties

  • Math.E: number [ES1]

Euler’s number, base of the natural logarithms, approximately 2.7182818284590452354.

  • Math.LN10: number [ES1]

The natural logarithm of 10, approximately 2.302585092994046.

  • Math.LN2: number [ES1]

The natural logarithm of 2, approximately 0.6931471805599453.

  • Math.LOG10E: number [ES1]

The logarithm of e to base 10, approximately 0.4342944819032518.

  • Math.LOG2E: number [ES1]

The logarithm of e to base 2, approximately 1.4426950408889634.

  • Math.PI: number [ES1]

The mathematical constant π, ratio of a circle’s circumference to its diameter, approximately 3.1415926535897932.

  • Math.SQRT1_2: number [ES1]

The square root of 1/2, approximately 0.7071067811865476.

  • Math.SQRT2: number [ES1]

The square root of 2, approximately 1.4142135623730951.

16.2. Exponents, roots, logarithms

  • Math.cbrt(x: number): number [ES6]

Returns the cube root of x.

  1. > Math.cbrt(8)
  2. 2
  • Math.exp(x: number): number [ES1]

Returns ex (e being Euler’s number). The inverse of Math.log().

  1. > Math.exp(0)
  2. 1
  3. > Math.exp(1) === Math.E
  4. true
  • Math.expm1(x: number): number [ES6]

Returns Math.exp(x)-1. The inverse of Math.log1p(). Very small numbers (fractions close to 0) are represented with a higher precision. This function returns such values whenever the result of .exp() is close to 1.

  • Math.log(x: number): number [ES1]

Returns the natural logarithm of x (to base e, Euler’s number). The inverse of Math.exp().

  1. > Math.log(1)
  2. 0
  3. > Math.log(Math.E)
  4. 1
  5. > Math.log(Math.E ** 2)
  6. 2
  • Math.log1p(x: number): number [ES6]

Returns Math.log(1 + x). The inverse of Math.expm1(). Very small numbers (fractions close to 0) are represented with a higher precision. This function receives such numbers whenever a parameter to .log() is close to 1.

  • Math.log10(x: number): number [ES6]

Returns the logarithm of x to base 10. The inverse of 10 ** x.

  1. > Math.log10(1)
  2. 0
  3. > Math.log10(10)
  4. 1
  5. > Math.log10(100)
  6. 2
  • Math.log2(x: number): number [ES6]

Returns the logarithm of x to base 2. The inverse of 2 ** x.

  1. > Math.log2(1)
  2. 0
  3. > Math.log2(2)
  4. 1
  5. > Math.log2(4)
  6. 2
  • Math.pow(x: number, y: number): number [ES1]

Returns xy, x to the power of y. The same as x ** y.

  1. > Math.pow(2, 3)
  2. 8
  3. > Math.pow(25, 0.5)
  4. 5
  • Math.sqrt(x: number): number [ES1]

Returns the square root of x. The inverse of x ** 2.

  1. > Math.sqrt(9)
  2. 3

16.3. Rounding

Rounding means converting an arbitrary number to an integer (a number without a decimal fraction). Tbl. 15 lists the available functions and what they return for a few representative inputs.

Table 15: Rounding functions of Math.
-2.9-2.5-2.12.12.52.9
Math.floor-3-3-3222
Math.ceil-2-2-2333
Math.round-3-2-2233
Math.trunc-2-2-2222
  • Math.ceil(x: number): number [ES1]

Returns the smallest (closest to −∞) integer i with xi.

  1. > Math.ceil(1.9)
  2. 2
  3. > Math.ceil(2.1)
  4. 3
  • Math.floor(x: number): number [ES1]

Returns the greatest (closest to +∞) integer i with ix.

  1. > Math.floor(1.9)
  2. 1
  3. > Math.floor(2.1)
  4. 2
  • Math.round(x: number): number [ES1]

Returns the integer that is closest to x. If the decimal fraction of x is .5 then .round() rounds up (to the integer closer to positive infinity):

  1. > Math.round(2.5)
  2. 3
  3. > Math.round(-2.5)
  4. -2
  • Math.trunc(x: number): number [ES6]

Removes the decimal fraction of x and returns the resulting integer.

  1. > Math.trunc(1.9)
  2. 1
  3. > Math.trunc(2.1)
  4. 2

16.4. Trigonometric Functions

All angles are specified in radians. Use the following two functions to convert between degrees and radians.

  1. function toRadians(degrees) {
  2. return degrees / 180 * Math.PI;
  3. }
  4. function toDegrees(radians) {
  5. return radians / Math.PI * 180;
  6. }
  • Math.acos(x: number): number [ES1]

Returns the arc cosine (inverse cosine) of x.

  1. > Math.acos(0)
  2. 1.5707963267948966
  3. > Math.acos(1)
  4. 0
  • Math.acosh(x: number): number [ES6]

Returns the inverse hyperbolic cosine of x.

  • Math.asin(x: number): number [ES1]

Returns the arc sine (inverse sine) of x.

  1. > Math.asin(0)
  2. 0
  3. > Math.asin(1)
  4. 1.5707963267948966
  • Math.asinh(x: number): number [ES6]

Returns the inverse hyperbolic sine of x.

  • Math.atan(x: number): number [ES1]

Returns the arc tangent (inverse tangent) of x.

  • Math.atanh(x: number): number [ES6]

Returns the inverse hyperbolic tangent of x.

  • Math.atan2(y: number, x: number): number [ES1]

Returns the arc tangent of the quotient y/x.

  • Math.cos(x: number): number [ES1]

Returns the cosine of x.

  1. > Math.cos(0)
  2. 1
  3. > Math.cos(Math.PI)
  4. -1
  • Math.cosh(x: number): number [ES6]

Returns the hyperbolic cosine of x.

  • Math.hypot(…values: number[]): number [ES6]

Returns the square root of the sum of the squares of values (Pythagoras’ theorem):

  1. > Math.hypot(3, 4)
  2. 5
  • Math.sin(x: number): number [ES1]

Returns the sine of x.

  1. > Math.sin(0)
  2. 0
  3. > Math.sin(Math.PI / 2)
  4. 1
  • Math.sinh(x: number): number [ES6]

Returns the hyperbolic sine of x.

  • Math.tan(x: number): number [ES1]

Returns the tangent of x.

  1. > Math.tan(0)
  2. 0
  3. > Math.tan(1)
  4. 1.5574077246549023
  • Math.tanh(x: number): number; [ES6]

Returns the hyperbolic tangent of x.

16.5. asm.js helpers

WebAssembly is a virtual machine based on JavaScript that is supported by most JavaScript engines.

asm.js is a precursor to WebAssembly. It is a subset of JavaScript that produces fast executables if static languages (such as C++) are compiled to it. In a way, it is also a virtual machine, within the confines of JavaScript.

The following two methods help asm.js and have few use cases, otherwise.

  • Math.fround(x: number): number [ES6]

Rounds x to a 32-bit floating point value (float). Used by asm.js to tell an engine to internally use a float value (normal numbers are doubles and take up 64 bits).

  • Math.imul(x: number, y: number): number [ES6]

Multiplies the two 32 bit integers x and y and returns the lower 32 bits of the result. Needed by asm.js: All other basic 32-bit math operations can be simulated by coercing 64-bit results to 32 bits. With multiplication, you may lose bits for results beyond 32 bits.

16.6. Various other functions

  • Math.abs(x: number): number [ES1]

Returns the absolute value of x.

  1. > Math.abs(3)
  2. 3
  3. > Math.abs(-3)
  4. 3
  5. > Math.abs(0)
  6. 0
  • Math.clz32(x: number): number [ES6]

Counts the leading zero bits in the 32-bit integer x. Used in DSP algorithms.

  1. > Math.clz32(0b01000000000000000000000000000000)
  2. 1
  3. > Math.clz32(0b00100000000000000000000000000000)
  4. 2
  5. > Math.clz32(2)
  6. 30
  7. > Math.clz32(1)
  8. 31
  • Math.max(…values: number[]): number [ES1]

Converts values to numbers and returns the largest one.

  1. > Math.max(3, -5, 24)
  2. 24
  • Math.min(…values: number[]): number [ES1]

Converts values to numbers and returns the smallest one.

  1. > Math.min(3, -5, 24)
  2. -5
  • Math.random(): number [ES1]

Returns a pseudo-random number n where 0 ≤ n < 1.

Computing a random integer i where 0 ≤ i < max:

  1. function getRandomInteger(max) {
  2. return Math.floor(Math.random() * max);
  3. }
  • Math.sign(x: number): number [ES6]

Returns the sign of a number:

  1. > Math.sign(-8)
  2. -1
  3. > Math.sign(0)
  4. 0
  5. > Math.sign(3)
  6. 1

16.7. Sources