JavaScript Editor JavaScript Debugger     JavaScript Editor 

Team LiB
Previous Section Next Section


The Math object holds a set of constants and methods enabling more complex mathematical operations than the basic arithmetic operators discussed in Chapter 4. You cannot instantiate a Math object as you would an Array or Date. The Math object is static (automatically created by the interpreter) so its properties are accessed directly. For example, to compute the square root of 10, the sqrt() method is accessed through the Math object directly:

var root = Math.sqrt(10);

Table 7-3 gives a complete list of constants provided by Math. A complete list of mathematical methods is given in Table 7-4.

Table 7-3: Constants Provided by the Math Object




The base of the natural logarithm (Euler's constant e)


Natural log of 2


Natural log of 10


Log (base 2) of e


Log (base 10) of e


Pi (p)


Square root of 0.5 (equivalently, one over the square root of 2)


Square root of 2

Table 7-4: Methods Provided by the Math Object




Absolute value of arg


Arc cosine of arg


Arc sine of arg


Arc tangent of arg

Math.atan2(y, x)

Angle between the x axis and the point (x, y), measured counterclockwise (like polar coordinates). Note how y is passed as the first argument rather than the second.


Ceiling of arg (smallest integer greater than or equal to arg)


Cosine of arg


e to arg power


Floor of arg (greatest integer less than or equal to arg)


Natural log of arg (log base e of arg)

Math.max(arg1, arg2)

The greater of arg1 or arg2

Math.min(arg1, arg2)

The lesser of arg1 or arg2

Math.pow(arg1, arg2)

arg1 to the arg2 power


A random number in the interval [0,1]


The result of rounding arg to the nearest integer. If the decimal portion of arg is greater than or equal to .5, it is rounded up. Otherwise, arg is rounded down.


Sine of arg


Square root of arg


Tangent of arg

There are several aspects of the Math object that need to be kept in mind. The trigonometric methods work in radians, so you need to multiply any degree measurements by p / 180 before using them. Also, because of the imprecise characteristic of floating-point operations, you might notice minor deviations from the results you expect. For example, though the sine of p is 0, the following code:


gives the result

This value is very close to zero, but just large enough to trip up sensitive calculations.

It might seem that Math does not provide the capability to compute logarithms in bases other than e. Indeed it does not, directly. However, the following mathematical identity

loga n = (loge n) / (loge a)

can be used to compute logarithms in an arbitrary base. For example, you can compute the log base 2 of 64 as

var x = Math.log(64) / Math.log(2);

Random Numbers

Because the Math.random() method returns values between zero and one, you must normalize its return value to fit the range of numbers required of your application. An easy way to get random integers in the range m to n (inclusive) is as follows:

Math.round(Math.random() * (n - m)) + m;

So to simulate a die roll you would use

roll = Math.round(Math.random() * (6 - 1)) + 1;

Generating random numbers in this manner is sufficient for most applications, but if “high quality” randomness is required, a more advanced technique should be used.

Easing Math Computations

When working extensively with the Math object, it is often convenient to use the with statement. Doing so allows you to use Math properties without prefixing them with “Math.” The concept is illustrated by the following example (computing the length of a side of a triangle with the Law of Cosines):

 with (Math) 
   var a = 3, b = 4, c;
   var angleA = atan(a / b);
   var angleB = atan(b / a);
   var angleC = PI / 2;
   c = pow(a, 2) + pow(b, 2) - 2 * a * b * cos(angleC);
   c = sqrt(c);

Team LiB
Previous Section Next Section

JavaScript Editor JavaScript Debugger     JavaScript Editor