Sitemap

Math object and some common methods

3 min readJul 11, 2020

--

In the last blog we discussed some common array methods in Javascript. Today, we will see some common methods available to us from Javascript Math object. When we are manipulating lots of numerical data, we will eventually have to do some advanced mathematical operations on them.

https://www.fcrsd.org/high_school/mathematics

Power, square-root and cube-root:

We can easily perform power function, square-root function and cube-root function using Math.pow(), Math.sqrt() and Math.cbrt() eg.

Since, the square-root can also be written as power of ½, we can use the Math.pow() to find the square-root also e.g.:

Note: If we use Math.pow() to determine the cube-root use power of ⅓, it gives us the approximate value because it is impossible to determine the actual value of ⅓.

Minimum and maximum number in the list:

Get Ashok Maharjan’s stories in your inbox

Join Medium for free to get updates from this writer.

Math.min() and Math.max() returns the minimum and the maximum numbers in the set.

Flooring, Ceiling and rounding:

Rounding is the process of approximating the decimal number to the nearest integer and we can implement that using Math.round(). eg.

Ceiling is a process where the number is always rounded up and during flooring it is always rounded down. eg.

And, if you are like me and would not want to worry about fractional digits, we can truncate using Math.trunc(). eg.

We can also generate a pseudo random number n where n>=0 and n<1, using Math.rand(). eg.

Since, the range of the rand() method is from 0 to 1, it does seem there is not much we can do, but with simple use of other math functions we can generate randoms number of other range quite easily. eg.

These are some of the common methods available to us to make out life little easier! Javascript also have other methods for Trigonometric functions like sin e and cosine but thats way over my head . If you want to learn more about all those higher order math functions please visit this page. So, that’s all for today, more about Javascript on the next blog!

--

--