Javascript Math

Math object provides several constants and methods to perform mathematical operation. Unlike date object, it doesn’t have constructors.


Math.sqrt(n) method

This method returns the square root of the given number.

Input:-
 <script>  
	  let squareRoot = Math.sqrt(225);  
    document.writeln("Square Root of 225 is ", squareRoot);
</script>

Output:-


Math.random() method

This method returns the random number between 0 to 1.

Input:-
 <script>  
    let randomNumber = Math.random();  
    document.writeln("Random Number is ", randomNumber);
</script>

Output:-


Math.pow(m,n) method

This method returns the m to the power of n that is mn.

Input:-
 <script>  
    let power = Math.pow(3,4);  
    document.writeln("3 to the power of 4 is ", power);
</script>

Output:-


Math.floor(n) method

This method returns the lowest integer for the given number. For example, 3 for 3.9, 5 for 5.7.

Input:-
 <script>  
    let floor = Math.floor(4.6);  
    document.writeln("Floor of 4.6 is ", floor);
</script>

Output:-


Math.ceil(n) method

This method returns the largest integer for the given number. For example, 5 for 4.7, 3 for 2.9.

Input:-
 <script>  
    let ceil = Math.ceil(4.7);  
    document.writeln("Ceil of 4.7 is ", ceil);
</script>

Output:-


Math.round(n)  method

This 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 – 5 for 4.9, 3 for 3.3.

Input:-
 <script>  
    let round = Math.round(4.9);  
    document.writeln("Round of of 4.9 is ", round);
</script>

Output:-


Math.abs(n) method

This method returns the absolute value for the given number. For example –  4 for -4, 7.6 for -7.6.

Input:-
 <script>  
    let absolute = Math.abs(-4.9);  
    document.writeln("Absolute value of of -4.9 is ", absolute);
</script>

Output:-