JavaScript Lesson 10: Math Object

<Lesson 9> [Contents] <Lesson 11>

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 see examples that use Math object in later lessons. These are various methods and properties of the Math object which we can use to perform mathematical calculations. These methods and properties are listed below:


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.

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)



The following example demonstrates the usage of Math methods in JavaScript.

Example 10.1

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Example 10.1</title>
</head>
<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>
</body>

</html>

The output is as follows:

Example 10.1 Using Math Methods

The absolute value of -5.12 is: 5.12
The value of acos0 is: 1.5707963267948966 in radian and 90 in degree
The value of asin(0.5) is: 0.5235987755982989 in radian and 30 in degree
The value of tan 45 is: 1
The value of sin 120 is: 0.8660254037844387
The value of cos240 is: -0.5000000000000004
The value of exp(3) is: 20.085536923187668
The value of ln(3) is: 1.0986122886681098
The value of greater number among 98 and 99 is: 99
The value of smaller number among 98 and 99 is: 98
The value of square root of 200 is: 14.142135623730951
The absolute value of -5.12 is: 5.12
The value of acos0 is: 1.5707963267948965 in radian and 90 in degree
The value of asin(0.5) is: 0.5235987755982989 in radian and 30 in degree
The value of tan 45 is: 1
The value of sin 120 is: 0.8660254037844387
The value of cos240 is: -0.5000000000000004
The value of exp(3) is: 20.085536923187668
The value of ln(3) is: 1.0986122886681097
The value of greater number among 98 and 99 is: 99
The value of smaller number among 98 and 99 is: 98
The value of square root of 200 is: 14.142135623730951

Click Example 10.1 to see the actual output to see the actual output

 

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:

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Math Properties</title>
</head>
<body>
<h1>Using Math properties</h1>
<script>

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>
</body>

The output is shown below:

Using Math properties

The value of e is: 2.718281828459045
The value of e is: 3
The value of ln10 is: 2.302585092994046
The value of ln2 is: 0.6931471805599453
The value of Pi is: 3.141592653589793
The value of Square root of 1/2 is: 0.7071067811865476
The value of Square root 2 is: 1.4142135623730951

The value of e in line 2 has been rounded off The value of e is: 2.718281828459045
The value of e is: 3
The value of ln10 is: 2.302585092994046
The value of ln2 is: 0.6931471805599453
The value of Pi is: 3.141592653589793
The value of Square root of 1/2 is: 0.7071067811865476
The value of Square root 2 is: 1.4142135623730951

The value of e in line 2 has been rounded off

Click Example 10.2 to see the actual output.



<Lesson 9> [Contents] <Lesson 11>