The JavaScript Math object offers a variety of constants and functions designed for executing mathematical calculations. In contrast to the date object, it does not include any constructors.
JavaScript Math Methods
Here is a compilation of JavaScript Math functions along with their explanations.
| Methods | Description |
|---|---|
| abs() | It returns the absolute value of the given number. |
| acos() | It returns the arccosine of the given number in radians. |
| asin() | It returns the arcsine of the given number in radians. |
| atan() | It returns the arc-tangent of the given number in radians. |
| cbrt() | It returns the cube root of the given number. |
| ceil() | It returns a smallest integer value, greater than or equal to the given number. |
| cos() | It returns the cosine of the given number. |
| cosh() | It returns the hyperbolic cosine of the given number. |
| exp() | It returns the exponential form of the given number. |
| floor() | It returns largest integer value, lower than or equal to the given number. |
| hypot() | It returns square root of sum of the squares of given numbers. |
| log() | It returns natural logarithm of a number. |
| max() | It returns maximum value of the given numbers. |
| min() | It returns minimum value of the given numbers. |
| pow() | It returns value of base to the power of exponent. |
| random() | It returns random number between 0 (inclusive) and 1 (exclusive). |
| round() | It returns closest integer value of the given number. |
| sign() | It returns the sign of the given number |
| sin() | It returns the sine of the given number. |
| sinh() | It returns the hyperbolic sine of the given number. |
| sqrt() | It returns the square root of the given number |
| tan() | It returns the tangent of the given number. |
| tanh() | It returns the hyperbolic tangent of the given number. |
| trunc() | It returns an integer part of the given number. |
Math.sqrt(n)
The JavaScript method math.sqrt(n) computes and returns the square root of the specified number.
Square Root of 17 is: <span id="p1"></span>
<script>
document.getElementById('p1').innerHTML=Math.sqrt(17);
</script>
Output:
Math.random
The JavaScript method math.random generates a random number that falls within the range of 0 to 1.
Random Number is: <span id="p2"></span>
<script>
document.getElementById('p2').innerHTML=Math.random();
</script>
Output:
Math.pow(m,n)
The JavaScript method math.pow(m, n) computes m raised to the power of n, which can be expressed as m^n.
3 to the power of 4 is: <span id="p3"></span>
<script>
document.getElementById('p3').innerHTML=Math.pow(3,4);
</script>
Output:
Math.floor(n)
The JavaScript method Math.floor(n) produces the largest integer that is less than or equal to the specified number. For instance, it will yield 3 for an input of 3.7, and 5 for an input of 5.9, among other similar examples.
Floor of 4.6 is: <span id="p4"></span>
<script>
document.getElementById('p4').innerHTML=Math.floor(4.6);
</script>
Output:
Math.ceil(n)
The JavaScript method math.ceil(n) yields the smallest integer that is greater than or equal to the specified number. For instance, it returns 4 for the input 3.7, and 6 for the input 5.9, among other examples.
Ceil of 4.6 is: <span id="p5"></span>
<script>
document.getElementById('p5').innerHTML=Math.ceil(4.6);
</script>
Output:
Math.round(n)
The JavaScript method Math.round(n) produces the closest integer to the specified number. When the decimal component is 0.5 or higher, it rounds up to the next whole number; conversely, if the decimal is less than 0.5, it rounds down to the nearest lower integer. For instance, it yields 4 for an input of 3.7, 3 for an input of 3.3, and 6 for 5.9, among other examples.
Round of 4.3 is: <span id="p6"></span><br>
Round of 4.7 is: <span id="p7"></span>
<script>
document.getElementById('p6').innerHTML=Math.round(4.3);
document.getElementById('p7').innerHTML=Math.round(4.7);
</script>
Output:
Math.abs(n)
The JavaScript function math.abs(n) is utilized to obtain the absolute value of a specified number. For instance, it yields 4 when provided with -4, and it produces 6.6 for an input of -6.6, among other similar cases.
Absolute value of -4 is: <span id="p8"></span>
<script>
document.getElementById('p8').innerHTML=Math.abs(-4);
</script>
Output: