Some Important Math Methods in JavaScript

ยท

4 min read

Hello Everyone,

I have tried to list down some of the most used Math methods in JavaScript, also remember this doesn't mean that you will only be using these methods, there are many methods in the Math Object and you may need to use some methods that I've not mentioned in this article for that you can always use the MDN documentation or any other resource to get help.

So, let's look at some of these methods, shall we?

1. Math.sqrt()

So, the very first method we are going to take a look at is the Math.sqrt() method, and well as the name suggests it is used when we need to calculate the square root of any number that we pass as an argument.

Note: All of these methods (which accept an argument) also perform type coercion implicitly. So, you can also pass numbers stored as a string to them and they will still give you the correct logical output. But they do not perform parsing and will give NaN as output if the argument can not be converted to a Number using type coercion.

Okay, let's take a look at some examples for Math.sqrt()

console.log(Math.sqrt(144)); // output: 12
console.log(Math.sqrt("144")); // output: 12
console.log(Math.sqrt("144px")); // output: NaN

2. Math.max() and Math.min()

We often find ourselves in a situation when we have to find the maximum/largest or minimum/smallest number out of given numbers or out of a given list/array. We can either pass any amount of Numbers separated by a comma (,) or pass an array of numbers as an argument. Let's understand better with a few examples

console.log(Math.max("12", 100, 99, 121, "122", 59)); // 122
console.log(Math.min("12", 100, 99, 121, "122", 59)); // 12
console.log(Math.max(12, "100px", 99, 121, "122", 59)); // NaN
console.log(Math.min(12, "100px", 99, 121, "122", 59)); // NaN
/* In the above you can see I have passed 6 values separated of which a few are numbers stored as a string and as I said 
earlier it will perform type coercion and show the correct output 
but as soon as it sees a value which can not be converted to a 
Number using type coercion simply gives NaN as output */

3. Math.trunc()

This method helps us remove everything after the decimal point from a Number, and only display the integer part of the given number. Now, remember that it by no means round off the number, it just simply removes the decimal part from the number and gives us just the integer part.

Example:

console.log(Math.trunc(12.321)); // 12
console.log(Math.trunc("12.9191")); // 12
console.log(Math.trunc("12.21xx")); // NaN

4. Math.random()

Math.random() can be helpful... well all these methods are really helpful that's why they are present in the JS library. But anyways, we use the random method to generate a random number between 0 and 1 (inclusive of 0 but not 1)

console.log(Math.random());
// prints a random number like 0.02880412484422501 every time you run
// ---------------------------------------------------------------- //
// random and trunc can be used together as shown below

// printing a random number between 1 and 6 (both inclusive)
console.log(Math.trunc(Math.random() * 6) + 1);

// printing a random number between any given range
const randomInt = (min, max) => 
    Math.trunc(Math.random() * (max - min) + 1) + min;

console.log(randomInt(100, 200)); // random number between 100 and 200

5. Math.round()

This method uses the conventional logic to round off a given decimal number and it rounds off the given number to the nearest integer

Example of this and the following two methods are shown together in the Math.floor() section of this article.

6. Math.ceil()

This method rounds up the given number to the nearest greatest integer.

7. Math.floor()

floor method is used to round down the given number to the nearest smallest integer.

Example:-

// round
console.log(Math.round(23.3)); // 23
console.log(Math.round(23.9)); // 24

// ceil
console.log(Math.ceil(23.3)); // 24
console.log(Math.ceil(23.9)); // 24

// floor
console.log(Math.floor(23.3)); // 23
console.log(Math.floor(23.9)); // 23

We can use Math.PI, if we want to use the value of Pi(ฯ€) anywhere in our program. I didn't mention it above because well it's not a method but it can be useful sometimes.

Conclusion

So, I have tried to tell you some of the important methods available to us via Math Object in JavaScript. But as I said it does not mean that other methods are not important or not useful, these are the methods that I've felt are used more frequently than others. Anyways, I hope this article was useful for you and if you think someone else might get help from it, then share this with them.

Happy Learning ๐Ÿ’ป๐Ÿ‘‹

ย