JavaScript The Math Object

please click here for more wordpress cource

The Math object is a built-in object in JavaScript that provides a collection of mathematical functions and constants. These functions and constants can be accessed using the Math object and can be used to perform various mathematical operations.

Here are some commonly used methods and constants of the Math object:

  1. Math.PI: This property represents the ratio of the circumference of a circle to its diameter and has a value of approximately 3.14159.
  2. Math.abs(x): This method returns the absolute value of a number x. For example, Math.abs(-5) returns 5.
  3. Math.ceil(x): This method returns the smallest integer greater than or equal to a number x. For example, Math.ceil(4.2) returns 5.
  4. Math.floor(x): This method returns the largest integer less than or equal to a number x. For example, Math.floor(4.8) returns 4.
  5. Math.round(x): This method returns the value of a number x rounded to the nearest integer. For example, Math.round(4.5) returns 5.
  6. Math.max(x1, x2, …, xn): This method returns the maximum value among a set of numbers. For example, Math.max(4, 2, 7, 1) returns 7.
  7. Math.min(x1, x2, …, xn): This method returns the minimum value among a set of numbers. For example, Math.min(4, 2, 7, 1) returns 1.
  8. Math.pow(x, y): This method returns the value of x raised to the power of y. For example, Math.pow(2, 3) returns 8.
  9. Math.sqrt(x): This method returns the square root of a number x. For example, Math.sqrt(25) returns 5.
  10. Math.random(): This method returns a random number between 0 and 1. For example, Math.random() may return 0.523458376197.

Note that all of these methods are static, which means that they are accessed through the Math object and not through an instance of it.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *