# Math Objects, Constants and Methods in Javascript

 SEARCH THIS SITE @import url(http://www.google.com/cse/api/branding.css); Custom Search

Math is an object defined in javascript and it has its math constants and methods that can be used in mathematical computations. Below are presented examples explaining the use of these constants and methods.

## Math Constants

We here present an example that allows you to access the euler constant e and the constant π. In the javascript example program 1 below, the euler constant is returned using Math.E and π is returned using Math.PI.
Rund the program and check that the result displayed is the euler constant is e ≈ 2.71....Use the alert to print pi and check that the displayed result is ≈ 3.14....

Javascript Example Program 1

<!DOCTYPE html>
<html>
<body>
<script>
var e = Math.E;
var pi = Math.PI;
</script>
</body>
</html>

## Square Root and Absolute Value Math Methods in Javascript

In example 2 below, we use the 2 mathods Math.sqrt(x), Math.abs(x) to find the square root and the absolute value of a real number x.
Run the sample program below and check that the result displayed is 2 (√4 = 2). Use Alert(ab) to display ab and check that the result displayed is 4 (|-4| = 4). Change the value of x to - 4. What does Math.sqrt(x) give? Why?

Javascript Example Program 2

<!DOCTYPE html>
<html>
<body>
<script>
var x = 4;
var sq = Math.sqrt(x);
var ab = Math.abs(-x);
</script>
</body>
</html >

## minimum and Maximum Math Methods in Javascript

In example 3 below, we use the 2 mathods Math.max(x1,x2,...), Math.min(y1,y2,...) to find the minimum and maximum value of a set of numbers x.
Run the sample program below and check that the result displayed is -8 (the minimum value in the set 0,-8,2,5). Change the displayed value to alert(max) and check that result displayed is 5.(the maximum value in the set 0,-8,2,5).

Javascript Example Program 3

<!DOCTYPE html>
<html>
<body>
<script>
var x = 4;
var mini = Math.min(0,-8,2,5);
var max = Math.max(0,-8,2,5);
</script>
</body>
</html >

## Power, Exponential and Logarithm base e Math Methods in Javascript

In example 4 below, we use the 3 mathods Math.pow(x,y), Math.exp(x) and Math.log(x) to find xy, ex and ln(x) respectively.
Run the sample program below and check that the result displayed is 64 ( = 43). Change the output to be displyed in alert() to expo and then ee and check that the displayed results are 7.389... (≈ e2) and 2 respectively and explain.

Javascript Example Program 4

<!DOCTYPE html>
<html>
<body>
<script>
var x = 4 , y = 2;
var ee = Math.E*Math.E;
var power = Math.pow(4,3);
var expo = Math.exp(2);
var logth = Math.log(ee);
</script>
</body>
</html >

## Round, Floor and Ceil Methods and Logarithm base e Math Methods in Javascript

In example 5 below, we apply the 3 methods
Math.round(x): round x to the nearest integer
Math.floor(x): round x downward to the nearest integer
Math.ceil(x): round x upward to the nearest integer
to show their effect on real numbers.
Run the sample program below and check that the result displayed is 4 ( 3.7 rounded to the nearest integer). Change the output to be displayed in alert() to flo and then cei and check that the displayed results are 3 ( = Math.floor(3.7) ) and 4 ( = Math.ceil(3.7) ) respectively and explain.

Javascript Example Program 5

<!DOCTYPE html>
<html>
<body>
<script>
var x = 3.7;
var rou = Math.round(x);
var flo = Math.floor(x);
var cei = Math.ceil(x);
</script>
</body>
</html >

## random() Method in Javascript

In example 6 below, we use Math.random() method to generate random numbers in the range 0 to 1.
Run the sample program below and check that the result displayed is a number between 0 and 1. Run it many times and check that the results displayed are different but the nmubers generated don follow any specific pattern (random) and their values are between 0 and 1.

Javascript Example Program 6

<!DOCTYPE html>
<html>
<body>
<script>
var ran = Math.random();
</script>
</body>
</html >

## Math Methods in Trigonometry

In example 7 below, we use the 3 trigonometric functions mathods: Math.sin(x), Math.cos(x) and Math.tan(x). The argument x is in radians.
Run the sample program below and check that the result displayed is -0.707... Change the displayed output to cosine and tangent and check that the results are successively 0.707... and -0.999...

Javascript Example Program 7

<!DOCTYPE html>
<html>
<body>
<script>
var x = - Math.PI/4; // x = -π/4
var sine = Math.sin(x);
var cosine = Math.cos(x);
var tangent = Math.tan(x);