Javascript Math functions reference

Javascript build-in Math object reference.

The Math object:

  • The Math object is a little different from the other built-in objects, because it CANNOT be used as a constructor to create objects.
  • It is also NOT possible to invoke the Math object as a function. ( eks.var x= Math(); )
  • It is just a collection of functions and constants.
  • But you can add new methods to the Math object as follows:
    <script type="text/javascript">
      // create a hypotenuse method on the Math object
      Math.hypotenuse = function(cat1, cat2) {
        var sumProd= cat1*cat1 + cat2*cat2;
        return Math.sqrt(sumProd);
      }
      document.write(Math.hypotenuse(24,32)+"<br>");
    </script>

General information about the object member types:

  1. Prototype created methods, properties or constants can only be used on instances of an object or on a primitive datatype.
  2. Constructor created methods, properties or constants can NOT be used on instances of an object or on a primitive datatype.

Constructor (Math) Methods:

Syntax: Math.abs(x)
The returning result has the same magnitude as x but has positive sign.
Return value: A number that is the absolute value of x.
An example of using the Math.abs() :
document.write(Math.abs(-5.678)+"<br>"); // prints:  5.678

Constructor (Math) Constants:

Math.E: The number value for e, the base of the natural logarithms, which is approximately 2.7182818284590452354.
Math.LN: The number value for the natural logarithm of 10, which is approximately 2.302585092994046.
Math.LN2: The number value for the natural logarithm of 2, which is approximately 0.6931471805599453.
Math.LOG2E: The number value for the base-2 logarithm of e, the base of the natural logarithms; this value is approximately 1.4426950408889634.
Math.LOG10E: The number value for the base-10 logarithm of e, the base of the natural logarithms; this value is approximately 0.4342944819032518.
Math.PI: The number value for Π (pi), the ratio of the circumference of a circle to its diameter, which is approximately 3.1415926535897932.
Math.SQRT1_2: The number value for the square root of 1/2, which is approximately 0.7071067811865476.
Math.SQRT2: The number value for the square root of 2, which is approximately 1.4142135623730951.

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.