Math.. our favorite! :D

Joseph Auz
2 min readDec 22, 2020
Photo by Jeswin Thomas on Unsplash

When I first got into the computer science world, I was super nervous about all of the math that would be involved in coding. There can be tons of math involved, but luckily it’s usually pretty simple in my situation. JavaScript even has quite a few built in tools to help with any math! Let’s take a look at some of them.

Math.PI    // returns 3.141592653589793

That’s easy enough. On to the next!

Math.round(4.7)      // returns 5
Math.round(4.3) // returns 4

Easy enough, we all know how to round.

Math.pow(8, 2)// returns 64 (8 * 8)

Now we’re starting to get to things that I might not be able to do in my head!

Math.sqrt(64)// returns 8

^^ A very simple example, but very useful!

Math.abs(-4.7)// returns 4.7

This returns the absolute value of the number you give it.

Math.ceil(4.7)// returns 5

This will return the value of what it is given rounded up to the nearest integer.

Math.floor(4.2)// returns 4

This will return the value of what it is given rounded down to the nearest integer.

Math.random()// returns a random number

Very very helpful for when you just need some test info.. But what else could it be used for?

Math.min(1, 132, 20, 15, -77, -243) // returns -243Math.max(1, 132, 20, 15, -77, -243)/returns 132

Helpful if you need to check multiple numbers and just pick out the single highest or lowest.

There are also some built in constants that are available for use off the bat.

Math.E        // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E

I don’t believe I’ve ever actually used any of these.. But in the situation that I do need them, I know for a fact that I will be extremely grateful for them.

Most of these seem pretty simple in their explanation, but when you think about the uses of them, and how you can incorporate them into logic, the options are endless!

I hope that you find these useful, and will try and find some fun ways to incorporate them into your projects. Happy coding!

--

--