0% completed
The Math object in JavaScript provides a collection of properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All its properties and methods are static
, meaning you access them using the Math prefix, not by creating a Math object instance. This object is extremely useful for performing mathematical tasks, ranging from basic arithmetic operations to complex calculations.
Since Math is a static object, you use it directly without instantiation:
let result = Math.methodName(parameters);
methodName
represents the Math method you wish to use.parameters
are the arguments that the method accepts.The Math object includes properties for mathematical constants:
Property | Description |
---|---|
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.303. |
Math.LOG2E | Base 2 logarithm of E, approximately 1.443. |
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. |
Math.SQRT1_2 | Square root of 1/2, approximately 0.707. |
Math.SQRT2 | Square root of 2, approximately 1.414. |
Math object methods for performing various mathematical operations:
Method | Description |
---|---|
Math.abs(x) | Returns the absolute value of x. |
Math.acos(x) | Returns the arccosine of x, in radians. |
Math.asin(x) | Returns the arcsine of x, in radians. |
Math.atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians. |
Math.atan2(y, x) | Returns the arctangent of the quotient of its arguments. |
Math.ceil(x) | Returns the smallest integer greater than or equal to x. |
Math.cos(x) | Returns the cosine of x (x is in radians). |
Math.exp(x) | Returns E<sup>x</sup>, where x is the argument, and E is Euler's constant (2.718...), the base of the natural logarithms. |
Math.floor(x) | Returns the largest integer less than or equal to x. |
Math.log(x) | Returns the natural logarithm (base E) of x. |
Math.max(x, y, z, ...) | Returns the number with the highest value. |
Math.min(x, y, z, ...) | Returns the number with the lowest value. |
Math.pow(x, y) | Returns the value of x to the power of y. |
Math.random() | Returns a random number between 0 (inclusive) and 1 (exclusive). |
Math.round(x) | Rounds x to the nearest integer. |
Math.sin(x) | Returns the sine of x (x is in radians). |
Math.sqrt(x) | Returns the square root of x. |
Math.tan(x) | Returns the tangent of an angle. |
Example 1: Calculating the Area of a Circle
Math.PI
and Math.pow()
for the radius squared.Example 2: Generating a Random Number
Example 3: Finding the Maximum and Minimum in a List of Numbers
Math.max()
and Math.min()
.Example 4: Rounding a Number
Math.round()
......
.....
.....