codescracker


javascript

JavaScript Math Object



« Previous Tutorial Next Tutorial »


The JavaScript Math object is used to perform simple and complex arithmetic operations.

JavaScript Math Object Properties

The following table lists the properties of the Math Object in JavaScript.

Property Description
E Euler's number (approx. value is 2.718)
LN2 natural logarithm of 2 (approx. value is 0.693)
LN10 natural logarithm of 10 (approx. value is 2.302)
LOG2E base-2 logarithm of E (approx. value is 1.442)
LOG10E base-10 logarithm of E (approx. value is 0.434)
PI numerical value of PI (approx. value is 3.142)
SQRT1_2 square root of 1/2 (approx. value is 0.707)
SQRT2 square root of 2 (approx. value is 1.414)

JavaScript Math Object Methods

Here is the table describes the methods of the Math object in JavaScript.

Method Description
abs(x) gives the absolute value of x
acos(x) gives arccosine of x (in radian)
asin(x) gives arcsine of x (in radian)
atan(x) gives the arctangent of x
atan2(y,x) gives the arctangent of the quotient on dividing y and x
ceil(x) rounds up x to the nearest bigger integer
cos(x) gives cosine value of x
exp(x) gives the value of ex
floor(x) rounds up x to the nearest smaller integer
log(x) gives the natural logarithmic value of x
max(x,y,z,...,n) gives the highest number from the given list
min(x,y,z,...,n gives the lowest number from the given list
pow(x,y) returns x to the power of y
random() returns a random number between 0 and 1
round(x) rounds up x to the nearest integer
sin(x) gives the sine value of x
sqrt(x) gives the square root of x
tan(x) gives the tangent value of x

JavaScript Math Object Example

Here is an example demonstrates Math object in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript Math Object</title>
</head>
<body>

<h3>JavaScript Math Object Example</h3>
<script type="text/javascript">
	document.write("Max(20, 30, 50, 10, 40) = " + Math.max(20, 30, 50, 10, 40) + "<br/>");
	document.write("Min(20, 30, 50, 10, 40) = " + Math.min(20, 30, 50, 10, 40) + "<br/>");
	document.write("Pow(2, 4) = " + Math.pow(2, 4) + "<br/>");
	document.write("Sqrt(64) = " + Math.sqrt(64) + "<br/>");
	document.write("Sin(90) = " + Math.sin(90) + "<br/>");
	document.write("Tan(45) = " + Math.tan(45) + "<br/>");
	document.write("Floor(20.8) = " + Math.floor(20.8) + "<br/>");
	document.write("Log(20.8) = " + Math.log(20.8) + "<br/>");
	document.write("Random() = " + Math.random() + "<br/>");
	document.write("Round(20.8) = " + Math.round(20.8) + "<br/>");
	document.write("Exp(10) = " + Math.exp(10) + "<br/>");
</script>

</body>
</html>

Here is the sample output produced by the above JavaScript Math Object example code:

javascript math object

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test