JavaScript Math
About JavaScript Math
JavaScript's Math is an object which provides a set of mathematical properties (constants) and methods (functions) for performing mathematical operations.
Unlike other objects, Math is not a constructor and all its properties and methods are static.
You can access its properties and methods directly like Math.property or Math.method().
Math Properties (Constants)
JavaScript Math's properties represent some important mathematical constants as mentioned below:
| Property | Value | Description |
|---|---|---|
| Math.E | ≈ 2.718 | Euler's number, base of the natural logarithm |
| Math.PI | ≈ 3.1416 | Ratio of a circle's circumference to its diameter |
| Math.SQRT2 | ≈ 1.414 | Square root of 2 |
| Math.SQRT1_2 | ≈ 0.707 | Square root of 1/2 |
| Math.LN2 | ≈ 0.693 | Natural logarithm of 2 |
| Math.LN10 | ≈ 2.302 | Natural logarithm of 10 |
| Math.LOG2E | ≈ 1.442 | Base-2 logarithm of E |
| Math.LOG10E | ≈ 0.434 | Base-10 logarithm of E |
The following example demonstrates how to access the Math's properties:
Math Methods (Functions)
JavaScript's Math provides several methods for performing different mathematical operations.
Rounding Methods
Rounding methods are used to round a number to the nearest integer.
Math.round(num): Returns the value of a number rounded to the nearest integer.Math.ceil(num): Returns the value of a number rounded up to its nearest integer.Math.floor(num): Returns the value of a number rounded down to its nearest integer.Math.trunc(num): Returns the integer part of a number by removing the decimal part.Math.fround(num): Returns the nearest 32-bit single precision float representation of a number.
The following example demonstrates how to use rounding methods:
Power and Root Methods
Power and root methods are used to calculate the power and root of a number.
Math.pow(x, y): Returns the value of x to the power of y ( e.g., Math.pow(2, 3) = 8).Math.sqrt(x): Returns the square root of x (e.g., Math.sqrt(16) = 4).Math.cbrt(x): Returns the cube root of x (e.g., Math.cbrt(27) = 3).Math.hypot(num1, num2, ...): Returns the square root of the sum of squares of its arguments (e.g., Math.hypot(3, 4) = 5).
The following example demonstrates how to use power and root methods:
Trigonometric Methods
Trigonometric methods are used to calculate the trigonometric values of an angle.
Math.sin(x): Returns the sine of x (in radians).Math.cos(x): Returns the cosine of x (in radians).Math.tan(x): Returns the tangent of x (in radians).Math.asin(x): Returns the arcsine of x (in radians).Math.acos(x): Returns the arccosine of x (in radians).Math.atan(x): Returns the arctangent of x (in radians).Math.atan2(y, x): Returns the arctangent of the quotient of its arguments.
The following example demonstrates how to use trigonometric methods:
Exponential and Logarithm Methods
Exponential and logarithm methods are used to calculate the exponential and logarithmic values of a number.
Math.exp(x): Returns the value of E^x (Euler's number raised to the power of x).Math.log(x): Returns the natural logarithm (base E) of x.Math.log10(x): Returns the base 10 logarithm of x.Math.log2(x): Returns the base 2 logarithm of x.
The following example demonstrates how to use exponential and logarithm methods:
Random Methods
Random methods are used to generate random numbers.
Math.random(): Returns a random number between 0 (inclusive) and 1 (exclusive).Math.random() * (max - min) + min: Returns a random number between min (inclusive) and max (exclusive).
The following example demonstrates how to use random methods:
Minimum and Maximum Methods
Minimum and maximum methods are used to find the minimum and maximum values of a set of numbers.
Math.min(num1, num2, ...): Returns the smallest of the numbers.Math.max(num1, num2, ...): Returns the largest of the numbers.
The following example demonstrates how to use minimum and maximum methods:
Hyperbolic Methods
Hyperbolic methods are used to calculate the hyperbolic values of a number.
Math.sinh(x): Returns the hyperbolic sine of x.Math.cosh(x): Returns the hyperbolic cosine of x.Math.tanh(x): Returns the hyperbolic tangent of x.Math.asinh(x): Returns the hyperbolic arcsine of x.Math.acosh(x): Returns the hyperbolic arccosine of x.Math.atanh(x): Returns the hyperbolic arctangent of x.
The following example demonstrates how to use hyperbolic methods:
Absolute & Sign Methods
Absolute and sign methods are used to calculate the absolute and sign values of a number.
Math.abs(x): Returns the absolute value of x.-
Math.sign(x): Returns the sign of x, indicating whether x is positive, negative, or zero (1 if positive -1 if negative and 0 if zero).
The following example demonstrates how to use absolute and sign methods:
Other Methods
Bitwise methods are used to perform bitwise operations on integers.
Math.clz32(x): Returns the number of leading zero bits in the 32-bit binary representation of number x.Math.imul(num1, num2): Returns the result of the C-like 32-bit multiplication of num1 and num2.
The following example demonstrates how to use other methods:
JavaScript Compiler For Live Practice
Practice the above JavaScript code in the following JavaScript Editor. Try more Math's methods in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.
JavaScript Editor