What is the Math Library?
This method rounds a number (usually a decimal) upwards to its nearest integer and returns the result. If the number x is an integer then there will be nothing to round.
Math.ceil(1.4) // -> 2
Math.ceil(2) // -> 2
This method is the opposite of Math.ceil(x). It rounds the decimal number downwards to the nearest integer and returns the result.
Math.floor(1.6) // -> 1
Math.floor(1) // -> 1
This is a property that returns the value of pi (3.14159….). This helps whenever you’re trying to calculate the circumference of a circle when it’s ever necessary. I used to be obsessed with the value of pi when I was a kid so I enjoy this property existing.
This method returns the value of x to the power of y. We are also able to use the ** operator to do the same thing. The issue with ** operand is that it isn’t compatible with Internet Explorer. Math.pow(x,y) gives us cross-browser support.
Math.pow(2,3) // -> 8
2 ** 3 // -> 8
This method checks if a number is negative, positive, or zero. If it’s negative, it returns -1. If it’s positive, it returns 1. If it’s zero, it returns 0. I found this method really useful on one of my previous LeetCode algorithm problems.
Math.sign(3) // -> 1
Math.sign(-5) // -> -1
Math.sign(0) // -> 0