# Math: Operators, Properties, and Methods JavaScript makes working with math easy. It provides us with the arithmetic operators you'd expect from a programming language, and offers us a cool Math library that does a lot of work for us so that we can focus on writing great code instead.

## Arithmetic Operators

Here are some of the many arithmetic operators that allow you to manipulate numbers in JavaScript.

```	```
let blueCars = 4;
let redCars = 5;

let totalCars = blueCars + redCars;

console.log("I have " + totalCars + " cars!");
```
```
```	```
I have 9 cars!
```
```

This is the shorthand way to add `1` to the value of a variable:

```	```
let burritos = 4;

console.log("I have " + burritos + " burritos!");
```
```
```	```
I have 5 burritos!
```
```

This is the shorthand way to add an arbitrary number to the value of a variable:

```	```
let books = 10;
books += 20; // adds 20 books

console.log("I have read " + books + " books!");
```
```
```	```
```
```

### Subtraction

```	```
// subtracting numbers
let bobsApples = 13;
let stolenApples = 7;

let remainingApples = bobsApples - stolenApples;

console.log("After getting robbed, he was left with " + remainingApples + " apples.");
```
```
```	```
After getting robbed, he was left with 6 apples.
```
```

This is the shorthand way to subtract `1` from the value of a variable:

```	```
let waterBottles = 4;
waterBottles--; // subtracts 1

console.log("I am down to " + waterBottles + " water bottles!");
```
```
```	```
I am down to 3 water bottles!
```
```

This is the shorthand way to subtract an arbitrary number from the value of a variable:

```	```
let apples = 50;
apples -= 30; // subtracts 30 apples

console.log("I have " + apples + " apples left!");
```
```
```	```
I have 20 apples left!
```
```

### Multiplication

```	```
// multiplying numbers
let tacosPerPerson = 3;
let people = 20;

let tacosNeeded = tacosPerPerson * people;

console.log("We need " + tacosNeeded + " tacos for the party.");
```
```
```	```
We need 60 tacos for the party.
```
```

This is the shorthand way to multiply the value of a variable:

```	```
let boxes = 2;
boxes *= 5; // multiplies the value by 5

console.log("I have " + boxes + " boxes left!");
```
```
```	```
I have 10 boxes left!
```
```

### Division

```	```
// dividing numbers
let totalSkittles = 75;
let kids = 5;

let skittlesPerKid = totalSkittles / kids;

console.log("Each kid gets " + skittlesPerKid + " skittles.");
```
```
```	```
Each kid gets 15 skittles.
```
```

This is the shorthand way to divide a variable:

```	```
let peanuts = 100;
peanuts /= 20; // divides the value by 20

console.log("I have " + peanuts + " peanuts left!");
```
```
```	```
I have 5 peanuts left!
```
```

### Modulus

```	```
// modulus operator
let people = 20;
let peoplePerCar = 6;

let peopleInLastCar = people % peoplePerCar;

console.log("The last car will have " + peopleInLastCar + " people in it.");
```
```
```	```
The last car will have 2 people in it.
```
```

This is the shorthand way to perform modulus:

```	```
let slices = 45;
slices %= 7; // does modulus 7

console.log("I have " + slices + " slices leftover!");
```
```
```	```
I have 3 slices leftover!
```
```

### Order of Operations

Order of Operations, also known as PEMDAS, is at play here, including with the use of parentheses.

```	```
// order of operations
let x = (5 * 3) + 10 / 2;

console.log("The value of x is " + x + ".");
```
```

## Math Properties

JavaScript's Math library, among other things, gives us access to mathematical properties that are commonly used so that we do not need to implement this ourselves.

### Euler's Number - Math.E

One of these constants is Euler's Number, via `Math.E`.

```	```
let euler = Math.E;
console.log(euler);
```
```
```	```
2.718281828459045
```
```

### Pi - Math.PI

Pi is the ratio of the circumference of a circle relative to its diameter, and is accessed in JavaScript using `Math.PI`.

```	```
let pi = Math.PI;
console.log(pi);
```
```
```	```
3.141592653589793
```
```

## Math Methods

Likewise with properties, JavaScript also implements some commonly used mathematical methods that we can use anytime we want.

### Absolute Value

Getting the absolute value of a number is easy. Simple pass a number to `Math.abs()` and it will return what you expect.

```	```
let a = 6;
let b = -13;
console.log("Absolute value of a is " + Math.abs(a) + ".");
console.log("Absolute value of b is " + Math.abs(b) + ".");
```
```
```	```
Absolute value of a is 6.
Absolute value of b is 13.
```
```

### Floor

The floor method rounds a number down to the nearest integer. Put a number inside `Math.floor()` and you're good to go.

```	```
let decimal = 24.942;
let floor = Math.floor(decimal);
console.log("The value of floor is " + floor + ".");
```
```
```	```
The value of floor is 24.
```
```

### Ceiling

The ceiling method rounds a number up to the nearest integer. Put a number inside `Math.ceil()` and you're good to go.

```	```
let decimal = 13.37;
let ceiling = Math.ceil(decimal);
console.log("The value of ceiling is " + ceiling + ".");
```
```
```	```
The value of ceiling is 14.
```
```

### Logarithm

You can get the natural logarithm of a number using the `Math.log()` method.

```	```
let number = Math.log(1);
console.log("The value of number is " + number + ".");
```
```
```	```
The value of number is 0.
```
```

### Maximum

You can get the maximum of two numbers using the `Math.max()` method.

```	```
let number = Math.max(14, 349);
console.log("The value of number is " + number + ".");
```
```
```	```
The value of number is 349.
```
```

### Minimum

You can get the minimum of two numbers using the `Math.min()` method.

```	```
let number = Math.min(53, 94);
console.log("The value of number is " + number + ".");
```
```
```	```
The value of number is 53.
```
```

### Power

You can take the power of a number by using `Math.pow()`.

```	```
let number = Math.pow(3, 2);
console.log("The value of number is " + number + ".");
```
```
```	```
The value of number is 9.
```
```

### Random

To get a random number in JavaScript, there's a `Math.random()` method for that. By itself, it just returns a number between `0` and `1`.

```	```
let random = Math.random();
console.log("The value of random is " + random + ".");
```
```
```	```
The value of random is 0.2156683652500082.
```
```

However, you can manipulate this fact to get a random number between two other numbers, like this:

```	```
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let random = randomBetween(5, 25);
console.log("The value of random is " + random + ".");
```
```
```	```
The value of random is 15.
```
```

### Round

You can round a number to the nearest integer using `Math.round()`.

```	```
let rounded = Math.round(3.14159);
console.log("The value of rounded is " + rounded + ".");
```
```
```	```
The value of rounded is 3.
```
```

### Square Root

You can take the square root of a number with `Math.sqrt()`.

```	```
let root = Math.sqrt(49);
console.log("The value of root is " + root + ".");
```
```
```	```
The value of root is 7.
```
```

That's all we got for math! 🤓🤓🤓