What is the use of Math object in JavaScript ?
The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance.
What is the Math Object?
The Math object is an inbuilt object that has attributes and methods for mathematical functions and constants. It’s not a function object. The Math object works with the Number type and does not have a constructor. All properties and methods of Math are fixed/static and can be called by using Math as an object without creating it.
For example, the cosine function is accessed using Math.cos(y), and the constant pi is accessed using Math.PI.
In this article, we will discuss various available methods & properties used in Javascript. We will start with the Math properties in Javascript.
Static Math Properties
The Math properties and their descriptions are listed below:
Syntax
Math.property
Property | Description | Return value |
---|---|---|
Math.E | Euler's constant, the base of natural logarithms is approximately 2.718. | Euler's number |
Math.LN2 | Natural logarithm of 2 which is approximately 0.693147180. | natural logarithm of 2 |
Math.LN10 | Natural logarithm of 10 which is approximately 2.302585. | natural logarithm of 10 |
Math.LOG2E | Base-2 logarithm of E which is approximately 1.442695. | base 2 logarithms of E |
Math.LOG10E | Base-10 logarithm of E which is approximately 0.43429844. | base 10 logarithms of E |
Math.PI | The ratio of the circumference of a circle to its diameter i.e. 3.14159. | PI value |
Math.SQRT1_2 | The square root of 1/2 is approximately 0.70710678. | The square root of 1/2 |
Math.SQRT2 | The square root of 2 is approximately 1.41421356. | The square root of 2 |
Example: Math Properties
console.log("Math.PI :" + Math.PI);
console.log("Math.SQRT2:" + Math.SQRT2);
console.log("Math.SQRT1_2:" + Math.SQRT1_2);
console.log("Math.LN10:" + Math.LN10);
console.log("Math.LN2:" + Math.LN2);
console.log("Math.LOG10E:" + Math.LOG10E);
console.log("Math.LOG2E:" + Math.LOG2E);
Output
Math.PI :3.141592653589793 Math.SQRT2:1.4142135623730951 Math.SQRT1_2:0.7071067811865476 Math.LN10:2.302585092994046 Math.LN2:0.6931471805599453 Math.LOG10E:0.4342944819032518 Math.LOG2E:1.44269504088...
Math Methods
The methods associated with the Math object are listed below, along with their descriptions.
Syntax
Math.method(number)
Method | Description |
---|---|
Math.abs(y) | The positive value of y is returned. |
Math.acos(y) | The arccosine of y is returned. |
Math.acosh(y) | Hyperbolic arccosine of y is returned. |
Math.asin(y) | The arcsine of y is returned. |
Math.asinh(y) | A number's hyperbolic arcsine is returned. |
Math.atan(y) | The arctangent of y is returned. |
Math.atanh(y) | Returns the hyperbolic arctangent of y. |
Math.atan2(y, x) | The arctangent of the quotient of its arguments is returned. |
Math.cbrt(y) | Returns the cube root of y. |
Math.ceil(y) | Returns the smallest integer greater than or equal to y. |
Math.clz32(y) | Returns the number of leading zero bits of the 32-bit integer y. |
Math.cos(y) | Returns the cosine of the angle y. |
Math.cosh(y) | Returns the hyperbolic cosine of y. |
Math.exp(y) | Returns e^(y), with x being the input and e being Euler's constant (2.718..., the natural logarithm's base). |
Math.expm1(y) | Returns subtracting 1 from exp(y). |
Math.floor(y) | Returns the largest integer less than or equal to y. |
Math.fround(y) | The nearest number's single-precision float representation is returned. |
Math.hypot([x[, y[, …]]]) | Returns the square root of the sum of squares of its parameters. |
Math.imul(x, y) | The result of the 32-bit integer multiplication of x and y is returned. |
Math.log(y) | Returns the natural logarithm of the number. |
Math.log1p(y) | For a number y, the natural logarithm of 1 + y is returned. |
Math.log10(y) | The base-10 logarithm of y. is returned. |
Math.log2(y) | The base-2 logarithm of y is returned. |
Math.max([x[, y[, …]]]) | Largest number is returned from x,y. |
Math.min([x[, y[, …]]]) | The smallest of all the numbers is returned from x,y. |
Math.pow(x, y) | Returns the exponent power y of the base value x (that is, x^y). |
Math.random() | An arbitrary number between 0 and 1 is returned. |
Math.round(y) | The value of y, rounded to the closest integer, is returned. |
Math.sign(y) | The sign of y, which indicates whether y is positive, negative, or zero is returned. |
Math.sin(y) | The sine of the angle y is returned. |
Math.sinh(y) | Hyperbolic sine of y is returned. |
Math.sqrt(y) | The positive square root of y is returned. |
Math.tan(y) | The tangent of y is returned. |
Math.tanh(y) | The hyperbolic tangent of y is returned. |
Math.trunc(y) | Removes all fractional digits from x and returns the integer part of it. |
Example: Math Methods
console.log("ceil :" + Math.ceil(9.6));
console.log("floor :" + Math.floor(9.6));
console.log("round :" + Math.round(9.6));
console.log("sine :" + Math.sin(30));
console.log("cosine :" + Math.cos(30));
console.log("min :" + Math.min(30, 40));
console.log("max :" + Math.max(30, 40));
console.log("sign :" + Math.sign(-40));
console.log("abs :" + Math.sign(-40));
Output
ceil :10 floor :9 round :10 sine :-0.9880316240928618 cosine :0.15425144988758405 min :30 max :40 sign :-1 abs :-1
Common Use Cases of Math Object
The Math object is widely used in various scenarios such as:
- Calculations: Performing arithmetic operations like addition, subtraction, multiplication, division, and more complex calculations involving trigonometric functions.
- Random Number Generation: Generating random numbers for simulations, games, or random selections.
const randomNumber = Math.random();
console.log("Random number:", randomNumber);