In Javascript Math object provides veriety of methods or function and properies that can be used to easily do mathematical calculations. Math object is an in-built object and so it is not required to instantiate the object as done for Date object.
Unlike the other global objects, Math is not a constructor. All properties and methods of Math are static.
Here is the simple syntax to call properties and methods of Math.
Syntax
<html>
<head>
<title>Javascript Math Object</title>
var log_val = Math.LOG10E;
var sine_val = Math.sin(10);
</head>
<body>
</body>
</html>
Here is a list of each math property with description.
Property | Description |
---|---|
E | Euler's constant and the base of natural logarithms, approximately 2.718. |
LN2 | Natural logarithm of 2, approximately 0.693. |
LN10 | Natural logarithm of 10, approximately 2.302. |
LOG2E | Base 2 logarithm of E, approximately 1.442. |
LOG10E | Base 10 logarithm of E, approximately 0.434. |
PI | Ratio of the circumference of a circle to its diameter, approximately 3.14159. |
SQRT1_2 | Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. |
SQRT2 | Square root of 2, approximately 1.414. |
Here is a list of each function of math object with description.
Method | Description |
---|---|
abs() | This will returns the absolute value of a number. |
acos() | This will returns the arccosine (in radians) of a number. |
asin() | This will returns the arcsine (in radians) of a number. |
atan() | This will returns the arctangent (in radians) of a number. |
atan2() | This will returns the arctangent of the quotient of its arguments. |
ceil() | This will returns the smallest integer greater than or equal to a number. |
cos() | This will returns the cosine of a number. |
exp() | This will returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm. |
floor() | This will returns the largest integer less than or equal to a number. |
log() | This will returns the natural logarithm (base E) of a number. |
max() | This will returns the largest of zero or more numbers. |
min() | This will returns the smallest of zero or more numbers. |
pow() | This will returns base to the exponent power, that is, base exponent. |
random() | This will returns a pseudo-random number between 0 and 1. |
round() | This will returns the value of a number rounded to the nearest integer. |
sin() | This will returns the sine of a number. |
sqrt() | This will returns the square root of a number. |
tan() | This will returns the tangent of a number. |
<DOCTYPE html>
<html>
<TITLE>Example of Number Object</TITLE>
<head>
<SCRIPT LANGUAGE="JavaScript">
document.write(Math.round(7.25));
document.write("</br>");
document.write(Math.random());
document.write("</br>");
var no=Math.random()*10
document.write(Math.floor(no))
document.write("</br>");
document.write(Math.max(2,4))
document.write("</br>");
document.write(Math.min(2,4))
document.write("</br>");
document.write(Math.ceil(4.4));
document.write("</br>");
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>