The Math object in JavaScript
Tags: javascript, beginners
Published: 2021-01-17 05:10:25

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.
```
Math.round(1.4);
// Returns 1

Math.round(2.5);
// Returns 3
```

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

```
Math.ceil(10.2);
// Returns 11

Math.ceil(13.7);
// Returns 14
```

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

```
Math.floor(100.3);
// Returns 100

Math.floor(56.9);
// Returns 56
```

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];

Math.max(...numberArray);
// Returns 9

Math.min(...numberArray);
// Returns 1
```

### 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. 

```
Math.sqrt(9);
// Returns 3

Math.sqrt(64);
// Returns 8
```

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
```

### 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.

```
Math.abs(-100);
// Returns 100

Math.abs(200);
// Returns 200
```

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.

```
Math.sign(-13);
// Returns -1

Math.sign(13);
// Returns 1

Math.sign(0);
// Returns 0
```

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: 

```
Math.E
// Returns Eulers constant which is 2.718

Math.PI
// Returns 3.14159 
```

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.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Anywho that'll it from me. Happy Coding!