Math Object

Javascript has a built in Math Object, which has its own properties and methods. The table below shows the properties of the Math Object with a brief explanation.


Property Explanation
E Value of Euler's constant (E) - approximate value of 2.71828
LN2 The natural logarithm of 2 - approximate value is 0.69315
LN10 The natural logarithm of 10 - approximate value is 2.30259
LOG2E The base 2 logarithm of E - approximate value is 1.442695
LOG10E The base 10 logarithm of E - approximate value is 0.43429
PI Represents the ratio of the circumference of a circle to its diameter - approximate value is 3.14
SQRT1_2 The square root of 0.5 - approximate value is 0.70711
SQRT2 The square root of 2 - approximate value is 1.41421



Actual Values

<script>

document.getElementById("v")
.innerHTML=

"E=" + Math.E + "<br>"+
"LN2=" + Math.LN2 + "<br>"+
"LN10=" + Math.LN10 + "<br>"+
"LOG2E=" + Math.LOG2E + "<br>"+
"LOG10E=" + Math.LOG10E + "<br>"+
"PI=" + Math.PI + "<br>"+
"SQRT2="+Math.SQRT2 + "<br>"+
"SQRT1_2 ="+Math.SQRT1_2+ "<br>"

</script>

Displays





Example: Math.PI

<script>
var radius=4;
var area;
document.write("Area of a circle is:π*r2")
document.write("<br><br>");
document.write(area=Math.PI*(radius*radius))
document.write("<br><br>");

//round the number up to the nearest integer
document.write("Area=" + Math.floor(area))
</script>


Displays






Method

The table below shows the methods of the Math Object with a brief explanation.


Methods Example Explanation Result
Math.abs() Math.abs(-4.2) returns the absolute value of -4.2
Math.acos() Math.acos(0.5) returns the arc cosine of 0.5
Math.asin() Math.asin(0.5) returns the arc sine of 0.5
Math.atan() Math.atan(3) returns the arc tangent of 3
Math.ceil() Math.ceil(6.85) rounds the number up
to the nearest integer
7
Math.cos() Math.cos(2) returns the cosine of 2
Math.exp() Math.exp(5) means e5 value of e is 2.718 returns the exponent value of 5
Math.floor() Math.floor(6.85) rounds the number down to the nearest integer 6
Math.log() Math.log(10) returns the natural logarithm of 10
Math.max() Math.max(2,3,-1,5) returns the largest number
from the list
5
Math.min() Math.min(2,3,-1,5) returns the smallest number
from the list
-1
Math.pow() Math.pow(2,4) means two to the power of 4 - (24) 2x2x2x2 16
Math.random() Math.random() returns a random number
Math.round() Math.floor(3.22) rounds the number to the nearest integer 3
Math.sin() Math.sin(2) returns the sine of 2
Math.sqrt() Math.sqrt(9) returns the square root of 9 3
Math.tan() Math.tan(45) returns the tangent of 45(angle)




Examples


Math.pow()

<script>
document.write("Math.pow(2,2) means:22-2x2=" + Math.pow(2,2))
document.write("<br><br>");
document.write("Math.pow(3,3) means:33-3x3x3=" + Math.pow(3,3))
document.write("<br><br>");
document.write("Math.pow(5,4) means:54-5x5x5x5=" + Math.pow(5,4))
</script>


Displays





Math.max() and Math.min()

<script>
document.write("Max" + Math.max(1,0.5,22,4) +"<br>");
document.write("Min" + Math.min(1,0.5,22,4) +"<br>");
</script>

Displays




Math.ceil() , Math.floor() and Math.round()

<script>
//rounds the number up to the nearest integer
document.write("Math.ceil " + Math.ceil(4.896));

//rounds the number down to the nearest integer
document.write("Math.floor " + Math.floor(4.896));

//rounds the number to the nearest integer
document.write("Math.round " + Math.round(4.896));
</script>

Displays




Math.sqrt()

<script>
document.write(Math.sqrt(16) + "<br>");
document.wr(Math.sqrt(81)++ "<br>");
document.write(Math.sqre(100)) + +"<br>");
</script>

Displays



Math.abs()

<script>
document.write(Math.abs(6*5) + "<br>");
document.write(Math.abs(-0.677) + "<br>");
document.write(Math.abs(0.9-0.2)+ "<br>");
</script>

Displays



Math.random()

<script>
document.write(Math.random() + "<br>" );
document.write(Math.random() + "<br>" );
document.write(Math.random() + "<br>" );
</script>

Displays




Returns a number between 0 and 21

<script>
document.write(Math.floor(Math.random()*22));
document.write(Math.floor(Math.random()*22));
document.write(Math.floor(Math.random()*22));
</script>

Displays




Returns a number between 1 and 22

<script>
document.write(Math.floor(Math.random()*22)+1);
document.write(Math.floor(Math.random()*22)+1);
document.write(Math.floor(Math.random()*22)+1);
</script>

Displays




Now do the Exercises!