Unleashing the Power of JavaScript Math Object: A Comprehensive Guide


JavaScript provides a built-in Math object that contains various mathematical functions and constants that can be used in your code. Some of the commonly used mathematical functions in JavaScript are

PI E round floor ceil
sqrt abs trunc (Return Integer only) pow min
max random random sign sin
cos log log2 log10
  • Math.abs(x): returns the absolute value of x, which is the non-negative value of x without any sign.
  • Math.ceil(x): rounds the number x up to the nearest integer.
  • Math.floor(x): rounds the number x down to the nearest integer.
  • Math.round(x): rounds the number x to the nearest integer.
  • Math.sqrt(x): returns the square root of x.
  • Math.pow(x, y): returns the value of x raised to the power of y.
  • Math.max(x1, x2, ...): returns the largest of zero or more numbers.
  • Math.min(x1, x2, ...): returns the smallest of zero or more numbers.
  • Math.random(): returns a random number between 0 and 1.
  • Math.sin(x): returns the sine of x (x is in radians).
  • Math.cos(x): returns the cosine of x (x is in radians).
  • Math.tan(x): returns the tangent of an angle x (x is in radians).
  • Math.log(x): returns the natural logarithm (base e) of x.
  • Math.log10(x): returns the common logarithm (base 10) of x.
  • Math.exp(x): returns the value of Ex.

The Math object also provides some mathematical constants like Math.PI, Math.E and Math.LN2, Math.LN10, Math.LOG2E, Math.LOG10E etc.

It's important to note that the Math object is a static object, which means that you do not need to create an instance of the object in order to use.

Source Code

HTML 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>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

This code demonstrates the use of the built-in Math object in JavaScript, which provides various mathematical functions and constants that can be used in your code.

  • The first line assigns the value of PI to the variable c using the Math.PI constant.
  • The next line assigns the value of E to the variable c using the Math.E constant.
  • The next line uses the Math.round() function to round the number 5.8 to the nearest integer, which is 6. The result is assigned to the variable c.
  • The next line uses the Math.floor() function to round the number 5.58 down to the nearest integer, which is 5. The result is assigned to the variable c.
  • The next line uses the Math.ceil() function to round the number 5.58 up to the nearest integer, which is 6. The result is assigned to the variable c.
  • The next line uses the Math.sqrt() function to find the square root of 90, which is 9.486832980505138. The result is assigned to the variable c.
  • The next line uses the Math.abs() function to find the absolute value of -45, which is 45. The result is assigned to the variable c.
  • The next line uses the Math.trunc() function to truncate the number 4.9 and returns the integer part, which is 4. The result is assigned to the variable c.
  • The next line uses the Math.pow() function to find the power of 2 raised to 4, which is 16. The result is assigned to the variable c.
  • The next line uses the Math.min() function to find the minimum number among 10, 50, 5, 45, and 8, which is 5. The result is assigned to the variable c.
  • The next line uses the Math.max() function to find the maximum
script.js
// Math Object
c=Math.PI;
c=Math.E;
c=Math.round(5.8);
c=Math.floor(5.58);
c=Math.ceil(5.58);
c=Math.sqrt(90);
c=Math.abs(-45);
c=Math.trunc(4.9);//Return Integer only
c=Math.pow(2,4);
c=Math.min(10,50,5,45,8);
c=Math.max(10,50,5,45,8);
c=Math.random();
c=Math.floor((Math.random()*50+1));
c=Math.sign(1); //Return Neg=-1 Zero=0 Pos=1
c=Math.sin(90);
c=Math.cos(90);
c=Math.log(1);
c=Math.log2(10);
c=Math.log10(10);
 
console.log(c)
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project