JavaScript Math Object

The JavaScript math object can be invoked without creating an instance of it.

Properties

Properties are invoked by lines like "Math.PI".

  • E - Euler's constant
  • LN2 - Natural log of the value 2
  • LN10 - Natural log of the value 10
  • LOG2E - The base 2 log of euler's constant (e).
  • LOG10E - The base 10 log of euler's constant (e).
  • PI - 3.1428 - The number of radians in a 360 degree circle (there is no other circle than a 360 degree circle) is 2 times PI.
  • SQRT1_2 - The square root of one half.
  • SQRT2 - The square root of 2.

Methods

Methods are invoked by lines like "Math.sin(1)".

  • abs(a) - Returns the absolute value of a. The result value is 23 in the example below.

    resultval = Math.abs(-23)

  • acos(a) - Returns the angle in radians that has a cosine of the passed value. The value of "resultval" is 90 degrees.

    resultval = (180/Math.PI) * Math.cos(0)

  • asin(a) - Returns the angle in radians that has a sine of the passed value. The value of "resultval" is 90 degrees.

    resultval = (180/Math.PI) * Math.sin(1)

  • atan(a) - Returns the angle in radians that has a tangent of the passed value. The "resultval" will be almost 90 degrees.

    resultval = (180/Math.PI) * Math.atan(100)

  • atan2(x,y) - Returns the polar coordinate angle based on cartesion x and y coordinates. The value is returned in radians. The "resultval" will be 45.

    resultval = (180/Math.PI) * Math.atan2(1,1)

  • ceil(x) - Rounds up the value of "a" to the next integer. If the value is a already whole number, the return value will be the same as the passed value. The example returns 13.

    resultval = Math.ceil(12.01)

  • cos(a) - Returns the cosine of "a" specified in radians. To convert radians to degrees, divide by 2*PI and multiply by 360. The example below returns the cosine of 90 degrees which is 0.

    resultval = Math.cos(90 * Math.PI/180)

  • exp(a) - Returns Euler's constant to the power of the passed argument. This is the exponential power. The example below will return a number between 9 and 10.

    resultval = Math.exp(3)

  • floor(a) - Rounds the passed value down to the next lowest integer. If the passed value is already an integer the returned value is the same as the passed value. The value returned in the example is 8.

    resultval = Math.floor(8.78)

  • log(a) - This function is the opposite of "exp()" returning the natural log of the passed value. In the below example, 3 is the result.

    resultval = Math.log(Math.exp(3))

  • max(a,b) - Returns the larger value of a or b. The variable "resultval" below becomes 5.

    resultval = Math.max(5, 3)

  • min(a,b) - Returns the lower value of a or b. The variable "resultval" below becomes 3.

    resultval = Math.min(5, 3)

  • pow(a,b) - Takes the value of a to the power b. In the example below the value 5 is multiplied times itself three times for a result of 125 which is placed in the variable "resultval".

    resultval = Math.pow(5, 3)

  • random() - Returns a random number between 0 and 1. To generate a number from 0 to 9:

    i1 = Math.round(Math.random() * 3)

  • round(a) - Returns the value of a to the nearest integer. If the values decimal value is .5 or greater the next highest integer value is returned otherwise the next lowest integer is returned. In the example below, 5 is returned.

    resultval = Math.round(5.3)

  • sin(a) - Returns the sine of "a" specified in radians. To convert radians to degrees, divide by 2*PI and multiply by 360. The example below returns the sine of 90 degrees which is 1.

    resultval = Math.sin(90 * Math.PI/180)

  • sqrt(a) - Returns the square root of a. the value of "resultval" is 5.

    resultval = Math.sqrt(25)

  • tan(a) - Returns the tangent of a value in radians which is the value of the sine divided by the cosine.

    resultval = Math.tan(0)