Maths in Javascript

Maths in Javascript

Maths in JavaScript are nothing but a namespace that contains several methods to use functions and properties.

The properties and methods of math functions are static. While they serve the same purpose for all engines, they might perform slightly different operations on different engines or web browsers. They are handy when it comes to building real-world applications and needs to be understood thoroughly.

Some of the maths functions in javascript are given below :

  1. Math.round():-

    The round method returns the nearest value of the integer if provided with a floating value. For example, it is expected to provide 3 if the number passed to it is 3.3. Similarly, it is expected to give 4 if the number passed to it is 3.9, and so on.

    Using math.round():-

console.log(Math.round(3.33));
//output=3
  1. Math.floor():-

    This function rounds off the value to the less than or equal to the decimal value

    entered. It is a self-explanatory function as the floor represents the lower ground so it rounds off the value to the lower integer in simple terms.

    Using Math.floor():-

     console.log(Math.floor(4.9));
     //output = 4
    
     console.log(Math.floor(4.2));
     //output = 4
    
  2. Math.ceil():-

    Likewise, the ceil function does the work of rounding off as well but to a greater integer than the decimal value entered.

     console.log(Math.ceil(4.9));
     //output = 5
    
     console.log(Math.ceil(4.2));
     //output = 5
    
  3. Min() and Max() function:-

    The min and max function does exactly what it says, find the minimum and maximum value from the numbers that are entered .

     //demonstrating min function
     console.log(Math.min(1,3,6,4,9));
     //output = 1
    
     //demonstrating max function 
     console.log(Math.max(1,3,6,4,9));
     //output = 9
    
  4. Random() function:-

    While developing real-world applications, there are situations in which a random numbers generator is required and the random() function does exactly that.

    It generates a random number that is greater than or equal to zero and less than 1. The value once generated does not match any other number that is about to be generated. So you could say it generates a unique number every single time.

      console.log(Math.random());
     //generates a random number between 0 to 1
    
     console.log(Math.random() * 10);
     //generates a random number between 0 to 1 and then multiplies it by 10 to get a new digit
    

    These are some of the most used javascript math functions. While these are very easy to learn and to use, they are also very important while developing real-world applications.