Blog

Mathematical functions and constants in JavaScript

This is post is about mathematical functions and constants in JavaScript. Most functions are part of the Math object.
The important constants and functions of the Math object are.
Constants

  • Math.E   gives Euler’s Number 2.718281828459045
  • MathLN2  gives natural log of 2 0.6931471805599453
  • Math.LN10 gives natural log of 10 2.302585092994046
  • Math.LOG2E gives base-2 logarithm of E 1.4426950408889634
  • Math.LOG10E gives base-10 log of E 0.4342944819032518
  • Math.PI gives PI 3.141592653589793
  • Math.SQRT1_2 gives square root of 1/2  = 0.7071067811865476
  • Math.SQRT2 gives square root of 2 = 1.4142135623730951

Functions

  • Math.abs() gives absolute value.  Math.abs(100=10, Math.abs(-10) = 10
  • Math.acos(value) gives cos inverse in radians Math.acos(-1) =π or 3.141592653589793
  • Math.asin(value) gives sin inverse in radians. Math.asin(1)=-π/2 or -1.5707963267948966
  • Math.atan(value) gives tan inverse in radians. Math.atan(infinity)= π/2 or 1.5707963267948966
  • Math.atan2(y,x) (-1,0) gives tan inverse of y/x. Math.atan2(-1,0) =π/2 or -1.5707963267948966
  • Math.ceil(x) gives the integer just larger or equal to x. Math.ceil(-11.2) =-11, or Math.ceil(11.3) =12, Math.ceil(11)=11.
  • Math.floor(x) gives the integer just less than or equal to x.
    Math.floor(11.2)=11, Math.floor(-11.2)=-12, Math.floor(-11)=-11;
  • Math.exp(x) gives ex thus Math.exp(1)=Math.E or 2.718281828459045
  • Math.log(x) gives natural log that is log(x)e. log(Math.E)=1
  • Math.max(x1,x2,x3,…xn) gives the largest number from the inputs. Math.max(11,22,3,4,-98)=22
  • Math.min(x1,x2,x3,…xn) gives the smallest number from the inputs. Math.min(11,22,3,4,-98)=-98
  • Math.random() gives a random number between 0 and 1. 0.32811933271750515 for instance
  • Math.sin(x), Math.cos(x) and Math.tan(x) give the value of the trignometrical functions where x is in radians.
    Math.sin(0)=0, Math.cos(0)=1, Math.tan(Math.PI/4)=0.9999999999999999
  • Math.sqrt(x) gives the square root of x. Math.sqrt(100)=10.