How to Use JavaScript’s Math Object to Program with HTML

The Math object is a special sort of object in JavaScript — you use it to access math-related functionality, rather than create new objects. As with any JavaScript object, you can create an instance of a Math object. However, you often use Math without creating an instance of it.

For example, you may simply need to add the value of pi to a calculation, in which case you can simply call on the appropriate property. The following list describes the Math object properties, all of which are available as static values (without creating an instance):

  • E: Provides the value of Euler’s number, which is approximately 2.718. (Many people know that this is the number e, which is the base of natural logarithms.)

  • LN2: Provides the value of the natural logarithm of 2, which is approximately 0.693.

  • LN10: Provides the value of the natural logarithm of 10, which is approximately 2.302.

  • LOG2E: Provides the value of the base-2 logarithm of the number e, which is approximately 1.442.

  • LOG10E: Provides the value of the base-10 logarithm of the number 3, which is approximately 0.434.

  • PI: Provides the value of pi (also shown as π), which is approximately 3.14.

  • SQRT1_2: Provides the value of the square root of 1/2, which is approximately 0.707.

  • SQRT2: Provides the value of the square root of 2, which is approximately 1.414.

The Math object also provides access to a number of methods. As with the properties, all of these methods are static, which means you can access them without creating a Math instance. The following list describes the Math object methods:

  • abs(x): Calculates the absolute value of x.

  • acos(x): Calculates the arccosine of x in radians. Check here to read more about trigonometric values.

  • asin(x): Calculates the arcsine of x in radians.

  • atan(x): Calculates the arctangent of x as a numeric value between -π/2 and π/2 radians.

  • atan2(y,x): Calculates the arctangent of the quotient of y divided by x.

  • ceil(x): Rounds the value of x up to the nearest integer value.

  • cos(x): Calculates the cosine of x, where the value of x is in radians.

  • exp(x): Calculates the value of ex (e to the power of x).

  • floor(x): Rounds the value of x down to the nearest integer value.

  • log(x): Calculates the natural logarithm (base e) of x.

  • max(x,y,z,...,n): Determines the number with the highest value and returns that value.

  • min(x,y,z,...,n): Determines the number with the lowest value and returns that value.

  • pow(x,y): Calculates the value of x to the power of y.

  • random(): Provides a random number between 0 and 1.

  • round(x): Rounds the value of x to the nearest integer value (either up or down as needed).

  • sin(x): Calculates the sine of x, where the value of x is in radians.

  • sqrt(x): Calculates the square root of x.

  • tan(x): Calculates the tangent of an angle.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com