/    /  Javascript-Math

Javascript-Math

 

Javascript Math is a built-in object. The math object provides has properties and methods for mathematical constants and functions. It’s not a function object.

The math works with the Number type. It doesn’t work with BigInt.

 

Syntax:

 

var pi_val = Math.PI;
var sine_val = Math.sin(30);

 

Static properties:

 

  • Math.E – Euler’s number(approximately 2.718).
  • Math.LN2 – Natural logarithm of 2(approximately 0.693).
  • Math.LN10 – Natural logarithm of 10(approximately 2.302).
  • Math.LOG2E – Base 2 logarithm of E(approximately 1.442).
  • Math.LOG10E – Base 10 logarithm of E(approximately 0.434).
  • Math.PI – Ratio( approximately 3.14159).
  • Math.SQRT1_2 -The square root of 1/2(approximately 0.707).
  • Math.SQRT2 – The square root of 2(approximately 1.414).

 

JavaScript Math Methods:

 

abs()                            Absolute value of the given number.

acos()                          Arccosine of the given number in radians.

asin()                           Arcsine of the given number in radians.

atan()                          Arc-tangent of the given number in radians.

cbrt()                           Cube root of the given number.

ceil()                            Smallest integer value, greater than or equal to the given number.

cos()                            Cosine of the given number.

cosh()                          Hyperbolic cosine of the given number.

exp()                            Exponential form of the given number.

floor()                          Largest integer value, lower than or equal to the given number.

hypot()                         Square root of sum of the squares of given numbers.

log()                             Natural logarithm of a number.

max()                           Maximum value of the given numbers.

min()                           Minimum value of the given numbers.

pow()                           Value of base to the power of exponent.

random()                     Random number between 0 (inclusive) and 1 (exclusive).

round()                                    Closest integer value of the given number.

sign()                           Sign of the given number

sin()                             Sine of the given number.

sinh()                           Hyperbolic sine of the given number.

sqrt()                           Square root of the given number

tan()                            Tangent of the given number.

tanh()                          Hyperbolic tangent of the given number.

trunc()                         Integer part of the given number.

 

Math.sqrt(n):

 

square root of the given number.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Square Root of 29 is: <span id="p1"></span>   
<script>   
document.getElementById('p1').innerHTML=Math.sqrt(29);   
</script>  
</body>
</html>

 

OUTPUT:

 

Square Root of 29 is: 5.385164807134504

 

Math.random():

 

random number between 0 to 1.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Random Number is: <span id="p2"></span>   
<script>   
document.getElementById('p2').innerHTML=Math.random();   
</script>  
</body>
</html>

 

OUTPUT:

 

Random Number is: 0.031442040719774855

 

Math.pow(m,n):

 

m to the power of n that is mn.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
6 to the power of 8 is: <span id="p3"></span>   
<script>   
document.getElementById('p3').innerHTML=Math.pow(6,8);   
</script>   
</body>
</html>

 

OUTPUT:

 

6 to the power of 8 is: 1679616

 

Math.floor(n):

 

Lowest integer for the given number.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Floor of 10.16 is: <span id="p4"></span>   
<script>   
document.getElementById('p4').innerHTML=Math.floor(10.16);   
</script> 
</body>
</html>

 

OUTPUT:

 

Floor of 10.16 is: 10

 

Math.ceil(n):

 

Largest integer for the given number.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Ceil of 10.16 is: <span id="p5"></span>   
<script>   
document.getElementById('p5').innerHTML=Math.ceil(10.16);   
</script>   
</body>
</html>

 

OUTPUT:

 

Ceil of 10.16 is: 11

 

Math.round(n):

 

Rounded integer nearest for the given number.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Round of 10.13 is: <span id="p6"></span><br>   
Round of 10.17 is: <span id="p7"></span>   
<script>   
document.getElementById('p6').innerHTML=Math.round(10.13);  
document.getElementById('p7').innerHTML=Math.round(10.17);   
</script>   
</body>
</html>

 

OUTPUT:

 

Round of 10.13 is: 10

Round of 10.17 is: 10

 

Math.abs(n):

 

Absolute value for the given number.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
Absolute value of -7 is: <span id="p8"></span>     
<script>     
document.getElementById('p8').innerHTML=Math.abs(-7);      
</script>
</body>
</html>

 

OUTPUT:

 

Absolute value of -7 is: 7