Using Math in JavaScript

Ronan McClorey
3 min readNov 28, 2020

--

Coming from a mathematical background with a Bachelor’s degree in Math, I find it interesting to see what JavaScript is able to create or solve mathematically. Math is a built-in object in JavaScript that has many properties and methods. These can be used in a variety of ways. Below I will show just a few of the many applications of the Math object.

Photo by Antoine Dautry on Unsplash

Numbers

One way the Math object can be used is to produce mathematical numbers a couple of examples of this are PI = 3.14159…. or Eulers Constant = 2.71828…. (There are several more). This code will return the number:

Math.PI
Math.E

Rounding

Math object can also be useful working with numbers that have decimal places. We can round the number to the nearest whole number with Math.round so

Math.round(2.4)

would return 2. Round works how you would regularly round numbers if the decimal number is less than .5 the number rounds down and .5 or greater rounds up.

This also isn’t the only way you can round a number you are also able to force a number to either round up to the nearest whole number or round down to the nearest whole number. The methods for this are as follows, in order to round down regardless of the value after the decimal point we use Math.floor.

Math.floor(2.1)
Math.floor(2.9)

So, both of the above methods would return 2.

If we on the other hand wanted to round up regardless of the number after the decimal instead we would use Math.ceil.

Math.ceil(2.1)
Math.ceil(2.9)

These methods as expected would both return 3.

Min & Max

We can find the smallest or largest number from a list of numbers using Math.min or Math.max respectively.

Math.min(2,7,1,9,5)
Math.max(2,7,1,9,5)

Math.min would return 1 and Math.max would return 9. These method will only work on lists of numbers provided after the method, however it is possible to make this method work with an array. In order to achieve this we have to call .apply after max or min that allows us to use an array as the second argument. For this specific use we don’t need to use the first argument of thisArg so we can place null here. Here is how we would use it

Math.max.apply(null, [2,7,1,9,5])
Math.min.apply(null, [2,7,1,9,5])

It will return that same as above this is simply using an array instead of a list of numbers.

Random

We can use Math to generate a random number with Math.random() this returns a random decimal number from 0 to less than 1. This usually isn’t exactly what people are usually looking for so you can easily change this to return a number between 0 and 10 or 0 and 100 which may be more commonly used. And we’ll use one of the other methods from above. So to get a random number from 0 to 100 the code would be:

Math.round(Math.random()*100)

These are only a few of the common Math functions I have used there are several others the MDN web docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math are a great resource to see these and more of what the Math object can do.

--

--

No responses yet