JavaScript Math Object - JavaScript Math Methods



JavaScript Math

  • The JavaScript math object provides several constants and methods to perform mathematical operation. Unlike date object, it doesn't have constructors.

JavaScript Math Methods

  • Let's see the list of JavaScript Math methods with description.
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 math.sqrt(n) method returns the square root of the given number.
javascript-math-sqrt

Sample Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
    
</head>
<body>
    Square Root of 64 is: <span id="p1"></span>    
    <script>    
    document.getElementById('p1').innerHTML=Math.sqrt(64);    
    </script>    
    
 
</body>
</html>

Output

Square Root of 64 is: 8

Math.random()

  • The JavaScript math.random() method returns the random number between 0 to 1
<span id="p2"></span>    
<script>    
document.getElementById('p2').innerHTML=Math.random();    
</script>   

Math.pow(m,n)

  • The JavaScript math.pow(m,n) method returns the m to the power of n that is mn.

Math.floor(n)

  • The JavaScript math.floor(n) method returns the lowest integer for the given number. For example 3 for 3.7, 5 for 5.9 etc.

Math.ceil(n)

  • The JavaScript math.ceil(n) method returns the largest integer for the given number. For example 4 for 3.7, 6 for 5.9 etc.

Math.round(n)

  • The JavaScript math.round(n) method returns the rounded integer nearest for the given number. If fractional part is equal or greater than 0.5, it goes to upper value 1 otherwise lower value 0. For example 4 for 3.7, 3 for 3.3, 6 for 5.9 etc.

Math.abs(n)

  • The JavaScript math.abs(n) method returns the absolute value for the given number. For example 4 for -4, 6.6 for -6.6 etc.
javascript-math-abs

Sample Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  
    </head>
<body>
    Square Root of          is: <span id="p1"></span>    <br>  
    Random Number           is: <span id="p2"></span>    <br>
    Floor of 45.6           is: <span id="p3"></span>    <br>
    Ceil of 76.6            is: <span id="p4"></span>    <br>
    Round of 12.3           is: <span id="p5"></span>    <br>
    Absolute value of -4    is: <span id="p6"></span>    <br>
    <script>    
    document.getElementById('p1').innerHTML=Math.sqrt(64);    
    document.getElementById('p2').innerHTML=Math.random();    
    document.getElementById('p3').innerHTML=Math.floor(45.6);    
    document.getElementById('p4').innerHTML=Math.ceil(76.6);    
    document.getElementById('p5').innerHTML=Math.round(12.3);   
    document.getElementById('p6').innerHTML=Math.abs(-4);      
    </script>    
</body>
</html>

Output

Square Root of  		is: 8
Random Number 		is: 0.9862446509440539
Floor of 45.6 		is: 45
Ceil of 76.6 			is: 77
Round of 12.3 		is: 12
Absolute value of -4 	is: 4

Related Searches to JavaScript Math Object - JavaScript Math Methods