Top 10 Math methods in JavaScript
In JS, Math is a built-in object that has several dozens of methods. Today we’ll focus on the most common of them, and I’m going to provide you with easy-to-understand examples.
The first 3 methods take in an integer and round it up but in different ways. Always think carefully when choosing which of them better suits your needs.
- Math.ceil() — the method’s name originates from the word ‘ceiling’. Think ‘up’ meaning we’ll round the number up. Be careful with negative numbers.
Math.ceil(5.1) // 6
Math.ceil(0.1) // 1
Math.ceil(0) // 0
Math.ceil(-0.1) // -0
Math.ceil(-3.9) // -3
2. Math.floor() — this method’s name originates from the word ‘floor’. Think ‘down’ meaning we’ll round the number down. Be careful with negative numbers.
Math.floor(5.1) // 5
Math.floor(0.1) // 0
Math.floor(0) // 0
Math.floor(-0.1) // -1
Math.floor(-3.9) // -4
3. Math.round() — this method returns a number rounded to the nearest integer (remember school Math classes?).
Math.round(5.1) // 5
Math.round(0.5) // 1
Math.round(0) // 0
Math.round(-0.1) // -0
Math.round(-3.9) // -4
4. Math.trunc() — this method’s name originates from the word ‘truncate’ and it simply removes any fractional digits (don’t confuse it with rounding up, it doesn’t round up anything):
Math.trunc(23.95) // 23
Math.trunc(-0.88) // -0
5. Math.max() — takes an array of integers as a parameter and returns the largest of them.
Math.max(10, 20, 100, 65.9) // 100
Math.max(-10, -20, -100, -65.9) // -10
Math.max(0, -1, 1, -1.1, 1.1) // 1.1
6. Math.min() — takes an array of integers as a parameter and returns the smallest of them.
Math.min(10, 20, 100, 65.9) // 10
Math.min(-10, -20, -100, -65.9) // -100
Math.min(0, -1, 1, -1.1, 1.1) // -1.1
7. Math.random() — this method helps you find a random number but doesn’t do it directly. Let me explain it better. The method itself doesn’t need an argument and returns a decimal number between 0 (inclusive) and 1 (exclusive):
Math.random() // 0.26645625999009925
If you then multiply it by another number, you’ll get a random number between this very number and 0:
Math.random()*10 // 6.153651095855553
8. Math.sqrt() — this method’s name originates from ‘square root’ and returns exactly that:
Math.sqrt(81) // 9
If the argument is negative, the result will be NaN:
Math.sqrt(-81) // NaN
9. Math.pow() — this method’s name originates from the word ‘power’ and takes an array of 2 parameters: the base (the first number) and the exponent (the second number). That is when you have something like this:
Math.pow(3, 5) // 243
It’s equal to 3*3*3*3*3.
There are several important things to remember here.
- If the exponent is 0, the result will always be 1, no matter what the base is:
Math.pow(-3, 0) // 1
- If the base is negative and even and the exponent is a positive integer, the result will be positive:
Math.pow(-3, 4) // 81
- If the base is negative and odd and the exponent is a positive integer, the result will be negative:
Math.pow(-3, 5) // -243
- If the base is negative and the exponent is positive decimal, the result will be NaN:
Math.pow(-3, 1.5) // NaN
- This is not the case if the base is positive:
Math.pow(3, 1.5) // 5.196152422706632
- If the exponent is negative, the result will contain lot of number after the . :
Math.pow(3, -1.5) // 0.19245008972987526
- If the base is negative and the exponent is negative, you’ll also get a bunch of numbers after . :
Math.pow(-3, -1) // -0.3333333333333333
- Beware that a negative base and a negative decimal exponent will result in NaN:
Math.pow(-3, -1.5) // NaN
OMG! Crazy Math.pow(), right?
10. Math.PI — is not a method but a property (which can be useful so I’ve decided to include it onto the list) and returns 3.141592653589793:
Math.PI * 2 // 6.283185307179586
I hope this article was useful and maybe even taught you something new. :)
What other JS Math methods (or properties) do you often use in your work?