JavaScript Math Objects

Math objects are built-in functions in JavaScript consisting of properties and methods that allows us to perform mathematical operations in our code. The Math function being an object has no constructor and can be called without creating it.

In this article, I will be highlighting some commonly used Math objects in JavaScript.

JS Math Objects

Rounding Functions

The following Math objects are used to perform rounding operations in JavaScript;

  • Math.round() : This takes in a parameter and rounds it to the nearest integer. e.g

     Math.round(6.9); // returns 7
    
     Math.round(6.4); // returns 6
    
  • Math.ceil() returns a value which is rounded up to its nearest integer. e.g

     Math.ceil(6.9); // returns 7
     Math.ceil(6.4); // returns 7
    
  • Math.floor() returns a rounded down value of the parameter it takes in. e.g
     Math.floor(6.9); // returns 6
    

Highest and Lowest Functions

  • Math.min() : This is a Math method that can be used to find the lowest or minimum values in a given list of arguments. e.g
    Math.min(2, 7, 4, 1, 3); // returns 1 being the lowest value.
    
  • Math.max() : Unlike Math.min(), this method returns the highest or maximum value. e.g
     Math.max(2, 7, 4, 1, 3); // returns 7 being the highest value.
    

Root Functions

  • Math.sqrt() : It returns the square root of a given parameter e.g
    Math.sqrt(16); // returns 4
    
  • Math.cbrt() returns the cubic root of its parameter e.g
    Math.cbrt(8); // returns 2
    

Random Function

  • Math.random() : This method returns a random decimal number between 0 and 1 excluding 1.
    Math.random(); // returns a random number
    

Power & Constant Functions

  • Math.pow() returns the value of a base to its power e.g
    Math.pow(2, 4); // returns 16, 2 being the base and 4 being its power
    
  • Math.PI It is a Math object property that returns the value of pi.
    Math.PI; // returns 3.141592653589793
    

Absolute Number Functions

  • Math.abs() returns an absolute positive value of a number. e.g
    Math.abs(-5); // returns 5
    

Trigonometric Functions

  • Math.sin() returns the sine values between -1 and 1 of an angle in radians.
    Math.sin(5); // returns -0.9589242746631384
    
  • Math.cos() returns the cosine values between -1 and 1 of an angle in radians. e.g
    Math.cos(5); // returns 0.2836621854632262
    
  • Math.tan() returns the tangent of an angle given in radians. e.g
    Math.tan(10); // returns 0.6483608274590866
    

To be able to work with degrees instead of radians in your code, you will need to do some conversions. You can do these conversions in your code like the example below.

   Math.sin(90 * Math.PI / 180);  // returns 1 which is the sine of 90 degrees

These methods can be very useful when working on your JavaScript projects for example a random password generator app. In addition, some methods can be combined to work together like the Math.floor() and Math.random().

   Math.floor(Math.random() * 100); // returns random numbers between 0 and 100

In the example given above, the Math.random() method will generate a random decimal number between 0 and 1 which is then multiplied by 100 to generate random decimal numbers between 0 and 100. Math.floor() then rounds down the generated random decimal number to an integer.

Summary

  • Math objects are very useful when it comes to performing mathematical calculations in our Javascript code.

I hope you find this article helpful!.

Thanks for reading ✨.

No Comments Yet