# Lesson 10: Math Object

The Math object is an important JavaScript object because it can be used to perform web-based mathematical calculations. It can compute data input by the user and then displays its output on the webpage. It is very useful for the programmer to design online applications that involve calculations and games. We will examine more examples that use Math object in later lessons. In addition, the Math object has methods and properties that can be used to performed customized calculations.

### 10.1 Math Methods

Math methods are also known as functions, they are indeed the built-in functions of JavaScript. The term method implies that the function belongs to a particular object. In fact, the terms method and function can be used interchangeably. Below is a list of methods that are tied to the Math object.

Table 10.1 Math Methods
Methods Description
abs(x) Returns absolute value of x
acos(x) Returns arc cosine of x (i.e cos-1x)
asin(x) Returns arc sine of x (i.e sin-1x)
atan(x) Returns arc tangent of x (i.e tan-1x)
cos(x) Returns the value of cosine x
sin(x) Returns the value of sine x
tan(x) Returns the value of tangent x
exp(x) Returns the value of e to the power of x (i.e  ex)
log(x) Returns the natural logarithm of x
max(x,y) Returns the greater value of x and y
min(x,y) Returns the greater value of x and y
round(x) Rounds off a number x to the nearest integer
pow(x,y) Returns x to the power of y  (i.e  xy)
sqrt(x) Returns the square root of x
ceil(x) Returns the least integer greater than or equal to x
floor(x) Returns the greatest integer less than or equal to x

The JavaScript syntax for the Math method is  Math.Method, where math must begin with the capital M. Using small m just will not work. For example, if you wish to compute the value of log 100, you write the code as Math.log(100), the value must always enclose within a bracket. In order to display the value on the web page, you can use the Document object together with the write method such as Document.write(Math.log(100)). For trigonometric calculations, you need to convert the values between radian and degree. For example, to compute the value of cos(60), you need to convert 60 to radian first otherwise you will get the wrong answer. The conversion can be done using the formula (60xPI)/180, written in JavaScript will be something like Math.cos((60*Math.PI)/180)

#### Example 10.1

```<title>Example 10.1</title>
<body>
<h2><font face="Arial">Example 10.1</font></h2>
<h1>Using Math Methods</h1>
<script language="javascript">
document.write (("The absolute value of -5.12 is : ")+Math.abs(-5.12));
document.write (("<br>The value of acos0 is : ")+Math.acos(0)+(" in radian")+(" and ")+(Math.acos(0)/Math.PI)*180+(" in degree"));
document.write (("<br>The value of asin(0.5) is : ")+Math.asin(0.5)+(" in radian")+(" and ")+Math.round((Math.asin(0.5)/Math.PI)*180)+(" in degree"));
document.write (("<br>The value of tan 45 is : ")+Math.round(Math.tan((45*Math.PI)/180)));
document.write (("<br>The value of sin 120 is : ")+Math.sin((120*Math.PI)/180));
document.write (("<br>The value of cos240 is : ")+Math.cos((240*Math.PI)/180));
document.write (("<br>The value of exp(3) is : ")+Math.exp(3));
document.write (("<br>The value of ln(3) is : ")+Math.log(3));
document.write (("<br>The value of greater number among 98 and 99 is : ")+Math.max(98,99));
document.write (("<br>The value of smaller number among 98 and 99 is : ")+Math.min(98,99));
document.write (("<br>The value of square root of 200 is : ")+Math.sqrt(200));
document.write("<br>")
</script>

```
The output is as follows:
<

### 10.2 Math Properties

Other than the aforementioned methods, the Math object also consists of a few properties which also represent certain constant values. These properties are listed in the following Table 10.2

Table 10.2 Math Properties
Property Description
E Euler's constant  ( the value is 2.718)
LN10 Natural logarithm of 10 (the value is 2.302)
LN2 Natural logarithm of 2 ( the value is 0.693)
LOG2E Logarithm to the base 2 of e  (log2e )
LOG10E Logarithm to the base 10 of e  (log10e )
PI The value of PI which is equal 3.14
SQRT1_2 The square root of half
SQRT2 The square root of 2

To use the Math properties, the syntax is

`Math.property.`

For example, Math.LN2 will give you the value of ln2 or loge2. Bear in mind that you have to use capital letter M for the Math object otherwise it will not compute. In order to display the value on a web page, you need to use the document object together with the write method. For example, you can use document.write(SQRT2) to display the value of square root of 2. The following example demonstrates the usage of Math properties to compute various mathematical values.

#### Example 10.2 Using Math Properties

```document.write (("The value of e is : ")+Math.E);
document.write (("<br>The value of e is : ")+Math.round(Math.E));
document.write (("<br>The value of ln10 is : ")+Math.LN10);
document.write (("<br>The value of ln2 is : ")+Math.LN2);
document.write (("<br>The value of Pi is : ")+Math.PI);
document.write (("<br>The value of Square root of 1/2 is : ")+Math.SQRT1_2);
document.write (("<br>The value of Square root 2 is : ")+Math.SQRT2);
document.write("<br>")
document.write("<br><em>The value of e in line 2 has been rounded off</em>");
</script>
```
The output is shown below: