DEV Community


Posted on

The Math object in JavaScript

I have recently been using the Math object and its methods in my algorithms and I can't help but wonder why I didn't start using it sooner. This may be a super basic topic to some people but I personally had little to no exposure to using it until recently and thought I'd go over a few of the useful methods just to get them out there.

One thing to clarify if you have no idea what I'm talking about JavaScript has a built in Math object with many methods to perform mathematical calculations or to provide a common mathematical value such as Pi or the constant e.

Now that we cleared that up let's get started.

1. Math.round(), Math.ceil() and Math.floor()

These are all very similar in that they round to the nearest whole integer.

Math.round() is used to round to the nearest whole integer whether it be lower or higher. Just like you would round UP at the .5 mark and down at anything lower.

// Returns 1

// Returns 3
Enter fullscreen mode Exit fullscreen mode

Math.ceil() also rounds to the nearest whole integer but it only rounds UP no matter the decimal value.

// Returns 11

// Returns 14
Enter fullscreen mode Exit fullscreen mode

Math.floor() does the opposite. It will always round DOWN to the nearest whole value.

// Returns 100

// Returns 56
Enter fullscreen mode Exit fullscreen mode

So they all have the same purpose but you have options depending on what you the situation might demand.

2. Math.min() and Math.max()

These do exactly what they seem like they will do and will return the minimum or maximum value of a set of data.

Don't let the simplicity fool you though that's incredibly useful.

My favorite way to use these is when determining the minimum or maximum value in an array. To do this all you have to do is pass the array into the method but first make sure to use the spread operator.

Math.min(100, 4, 13, 8,56);
// Returns 4

Math.max(10, 2000, 26, 1701, 235);
// Returns 2000

const numberArray = [3, 6, 1, 4, 9];

// Returns 9

// Returns 1
Enter fullscreen mode Exit fullscreen mode

3. Math.sqrt() and Math.pow()

Math.sqrt() allows you to find the square root of a given value. Nothing to fancy but definitely handy to know.

// Returns 3

// Returns 8
Enter fullscreen mode Exit fullscreen mode

Math.pow() takes in two values. The first being the base value, the second the power you'd like to apply to it.

Math.pow(2, 2);
// Returns 4

Math.pow(2, 8);
// Returns 256
Enter fullscreen mode Exit fullscreen mode

4. Math.abs() and Math.sign()

Math.abs() gives you the absolute value of a number so if you have a case where you need a negative to be it's positive counterpart you use this, which happened to me recently incidentally.

// Returns 100

// Returns 200
Enter fullscreen mode Exit fullscreen mode

Math.sign() tells you the sign of the input whether it be positive, negative or zero. It returns it in the form of -1, 0, or 1.

// Returns -1

// Returns 1

// Returns 0
Enter fullscreen mode Exit fullscreen mode

This has proven to be useful for testing if a value passes a threshold.

Wrap Up

These are all methods I've recently used in my algorithm practice that have helped tremendously. They've also helped me shorten my solutions in multiple cases.

There are quite a few methods used for calculation purposes and a couple common values like:

// Returns Eulers constant which is 2.718

// Returns 3.14159 
Enter fullscreen mode Exit fullscreen mode

Really though there a ton of methods to use on the object for your calculation needs and here's a reference for your future mathematical needs.

Anywho that'll it from me. Happy Coding!

Top comments (1)

nazanin_ashrafi profile image
Nazanin Ashrafi

Pretty helpful article thank you