JavaScript Math object
In this tutorial you will learn how to work with various math constants and methods.
The math object provides you properties and methods for mathematical Constants (properties) like PI, E, SQRT2 etc and functions(methods) likemin,max, abs, sin etc.
Math object properties:
- Math.E Euler's constant and the base of natural logarithms, approximately 2.718.
- Math.LN2 Natural logarithm of 2, approximately 0.693.
- Math.LN10 Natural logarithm of 10, approximately 2.302.
- Math.LOG2E Base 2 logarithm of E, approximately 1.442.
- Math.LOG10E Base 10 logarithm of E, approximately 0.434.
- Math.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.
- Math.SQRT2 Square root of 2, approximately 1.414.
Math Methods
- Math.abs() Returns the absolute value of a number.
- Math.exp() Returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm.
- Math.random() Returns a pseudo-random number between 0 and 1.
- Math.round() Returns the value of a number rounded to the nearest integer.
- Math.floor() Returns the largest integer less than or equal to a number.
- Math.ceil() Returns the smallest integer greater than or equal to a number.
- Math.log() Returns the natural logarithm (base E) of a number.
- Math.max() Returns the largest of zero or more numbers.
- Math.min() Returns the smallest of zero or more numbers.
- Math.pow() Returns base to the exponent power, that is, baseexponent.
- Math.sin() Returns the sine of a number.
- Math.cos() Returns the cosine of a number.
- Math.sqrt() Returns the square root of a number.
- Math.tan() Returns the tangent of a number.
- Math.acos() Returns the arccosine (in radians) of a number.
- Math.asin() Returns the arcsine (in radians) of a number.
- Math.atan() Returns the arctangent (in radians) of a number.
- Math.atan2() Returns the arctangent of the quotient of its arguments.
- Math.toSource() Returns the string "Math".
Example
var area =(Math.PI)* radius * radius; document.write(Math.abs(-1));// Prints: 1 document.write(Math.abs(1));// Prints: 1 document.write(Math.abs(-5));// Prints: 5 document.write(Math.abs(-10.5));// Prints: 10.5 Math.floor(Math.random()* max); document.write(Math.sqrt(4));// Prints: 2 document.write(Math.sqrt(16));// Prints: 4 document.write(Math.ceil(3.5));// Prints: 4 document.write(Math.ceil(-5.7));// Prints: -5 document.write(Math.ceil(9.99));// Prints: 10 document.write(Math.ceil(-9.99));// Prints: -9 document.write(Math.ceil(0));// Prints: 0 document.write(Math.floor(3.5));// Prints: 3 document.write(Math.floor(-5.7));// Prints: -6 document.write(Math.floor(9.99));// Prints: 9 document.write(Math.floor(-9.99));// Prints: -10 document.write(Math.floor(0));// Prints: 0 document.write(Math.round(3.5));// Prints: 4 document.write(Math.round(-5.7));// Prints: -6 document.write(Math.round(7.25));// Prints: 7 document.write(Math.round(4.49));// Prints: 4 document.write(Math.round(0));// Prints: 0 document.write(Math.max(1,3,2));// Prints: 3 document.write(Math.max(-1,-3,-2));// Prints: -1 document.write(Math.min(1,3,2));// Prints: 1 document.write(Math.min(-1,-3,-2));// Prints: -3 document.write(Math.pow(3,2));// Prints: 9 document.write(Math.pow(0,1));// Prints: 0 document.write(Math.pow(5,-2));// Prints: 0.04 document.write(Math.pow(16,0.5));// Prints: 4 (square root of 4) document.write(Math.pow(8,1/3));// Prints: 2 (cube root of 8) document.write(Math.sin(0*Math.PI/180));// Prints: 0 document.write(Math.sin(90*Math.PI/180));// Prints: 1 document.write(Math.cos(0*Math.PI/180));// Prints: 1 document.write(Math.cos(180*Math.PI/180));// Prints: -1 document.write(Math.tan(0*Math.PI/180));// Prints: 0