Programming Tutorials

Math in JavaScript

By: aathishankaran in JavaScript Tutorials on 2007-03-29  

For mathematical calculations, JavaScript encapsulates a host of mathematical constants and procedures into a single entity-the Math object. The Math object is quite a bit different from the other built-in objects. First, you can perform basic arithmetic calculations (addition, sub-traction, multiplication, and division) outside a Math object, so unless you regularly require advanced math functions, you might rarely use it.

Few developers might need to tap into the true power of the Math object, but for those who do, its capabilities are vital. Rather than pass values back to the server to perform mathematical calculations, you can use the Math object to perform advanced math processing on the client side.

Second, although you can create instances of String, Array, and Date objects using new, you work with a built-in instance of the Math object. This quality of the Math object parallels the Navigator object, for example, which is not created on-the-fly. Consequently, the Math object is commonly referred to as a static object.

Do not confuse the Math object as a "number" object. The Math object is used for calculations only; it is not intended to be used as a representative for a number as a String object is commonly used in place of a string literal. It can be confusing because unlike the string, there is no number object per se. A number (either an integer or floating point) is recognized as a distinct data type, but it is not an object type.

The Math object's properties are really nothing more than a list of common mathematical constants. Note the case of the constants. Although nearly all JavaScript properties are typically lowercase or mixed case, these properties are all uppercase.

Math object properties

Property Returns Description

E

LN2

LN10

LOG2E

LOG10E

PI

SQRT1_2

SQRT2

2.718281828

0.693147181

2.302585093

1.442695041

0.434294482

3.141592654

0.707106781

1.414213562

Euler's constant

Natural log of 2

Natural log of 10

Log base -2 of E

Log base -10 of E

Pi

Square root of 0.5

Square root of 2

 

Method Returns

abs(value)

acos(value).

asin(vaJue)

atan(value)

ceil(value)

cos(value)

exp(value)

floor(value)

log(value)

max(value1, value2)

min(value1, value2)

pow(value1, value2)

random()

round(value)

sin(value)

sqrt(value)

tan(value)

Absolute value of value

Arc cosine of value (in radians)

Arc sine of value (in radians)

Arc tangent of value (in radians)

Next integer >= value

Cosine of value

Euler's constant to the power of value

Next integer <= value

Natural logarithm of value (base e)

The highest number of value1 or value2

The lowest number of value1 or value2

value1 to the value2 power

A random number between 0 and 1

n + 1 (if value >= n.5; else n)

Sine of value (in radians)

Square root of value

Tangent of value (in radians)

Example:

<Html>
<Head>
  <Title>Date Object</Title>
</Head>

<Body>
  <H1>Math Object</H1>
  <Hr>
  <Script>
    document.write("<B>");
    document.write("<Br>"); document.write("<Br>");
    document.write("Absolute value of value : " + Math.abs(-10));
    document.write("<Br>"); document.write("<Br>");
    document.write("Arc cosine of value (in radians) : " + Math.acos(.5));
    document.write("<Br>"); document.write("<Br>");
    document.write("Arc sine of value (in radians) : " + Math.asin(O.5));
    document.write("<Br>"); document.write("<Br>");
    document.write("Arc tangent of value (in radians) :" + Math.atan(0.5));
    document.write("<Br>"); document.write("<Br>");
    document.write("Next integer >= value: " + Math.ceil(10.4));
    document.write("<Br>"); document.write (" < Br > ");
    document.write("Cosine of value: " + Math.cos(90));
    document.write("<Br>"); document.write("<Br>");
    document.write(" Euler's constant to the power of value: " + Math.exp(5));
    document.write("<Br>"); document.write("<Br>");
    document.write("Next integer <= value: " + Math.floor(10.4));
    document.write("<Br>"); document.write("<Br>");
    document.write(" Natural logarithm of value (base e) :" + Math.log(20));
    document.write("<Br>"); document.write("<Br>");
    document.write("The highest number of value1 or value2 " + Math.max(9540, 10000));
    document.write("<Br>"); document.write("<Br>");
    document.write(" The lowest number of value1 or value2 : " + Math.min(500, 450));
    document.write("<Br>"); document.write("<Br>");
    document.write(" value1 to the value2 power " + Math.pow(2, 2));
    document.write("<Br>"); document.write("<Br>");
    document.write("A random number between 0 and 1 :" + Math.random());
    document.write("<Br>"); document.write("<Br>");
    document.write("n + 1 (if value >= n.5; else n) : " + Math.round(10.99));
    document.write("<Br>"); document.write("<Br>");
    document.write(" Sine of value (in radians) " + Math.sin(90));
    document.write("<Br>"); document.write("<Br>");
    document.write("Square root of value: " + Math.sqrt(9));
    document.write("<Br>"); document.write("<Br>");
    document.write("Tangent of value (in radians) " + Math.tan(90));
    document.write("<Br>"); document.write("<Br>");
    document.write("<hr>");
    document.write("</B>");
  </Script>
</Body>
</Html>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JavaScript )

Latest Articles (in JavaScript)