Built-in objects:

# Math

When you need to do more complex mathematical operations than the ones we have already discussed, like basic addition, subtraction, multiplications and so on, then you will quickly appreciate the built-in Math object. It contains a lot of methods for doing more heavy mathematical stuff, as well as some useful properties.

Unlike several of the other built-in objects we have already discussed in this chapter, the Math object contains only static members. In other words, you can't just call the Math-methods on e.g. a Number (object). Instead, you will have to reference the properties and methods on the Math object, while passing in the number(s) you want to work on. It also means that you can't create a new Math object or call it like a function, as we've seen with some of the other built-in objects.

With that in mind, let's examine what kind of useful properties and methods we can expect from the Math object.

## Math properties

The Math object contains several properties which are useful for different aspects of doing advanced mathematical operations. Since they directly relate to actual math, and are therefore not specific to JavaScript, I won't be showing you examples of all of them. Instead, here's a quick list, with links to their reference, in case you want to dig deeper into them:

• Math.E - Euler's number (the base of natural logarithms)
• Math.LN10 - the natural logarithm of 10 - approximately 2.302
• Math.LN2 - the natural logarithm of 2 - approximately 0.693
• Math.LOG10E - the base 10 logarithm of e - approximately 0.434
• Math.LOG2E - the base 2 logarithm of e - approximately 1.442.
• Math.PI - approximately 3.14159, representing the ratio of the circumference of a circle to its diameter
• Math.SQRT1_2 - the square root of 1/2 (approximately 0.707)
• Math.SQRT2 - the square root of 2 (approximately 1.414)

With that in place, let's talk about the methods of the Math object.

## Math methods

The Math object contains a bunch of useful static methods, but as mentioned, currently no instance methods.

Some of the static methods found on the Math object directly relates to mathematical functions, like Math.cos() and Math.sin() (cosine and sine) - I will skip these methods, as they are basically more related to math than JavaScript. For a full list of the methods found on the Math object, please see this reference.

With that in mind, here's a list of the most relevant and interesting methods found on the Math object.

### Math.ceil()

The ceil() method will round up (if necessary) a number to the nearest integer. Here's a couple of examples to show how it works:

``````// Result: 4 (already an integer)

// Result: 1

// Result: 7

// Result: 8

The fact that it always rounds up the number, and never down, might not make it the obvious choice for most rounding requirements. The Math.round() method is more commonly used - we'll discuss it below.

### Math.floor()

While the ceil() method will always round up, the Math.floor() method will always round down to the nearest integer. Here's some examples to show it in action:

``````// Result: 4 (already an integer)

// Result: 0

// Result: 6

// Result: 7

And again, if you're looking for a method which will round up or down, and not always down, have a look at Math.round() below.

### Math.max()

The max() method will take an unlimited (but minimum 2) numbers and return the highest one. It works for both integers and floating point numbers. Here's how it can be used:

``````// Result: 2

// Result: 8

// Result: 0.97

// Result: 10.01

### Math.min()

While the max() method returns the biggest number, the min() method will do the opposite and return the smallest number. And just like the max() method, it works with both integers and floating point numbers. Here are some examples:

``````// Result: 1

// Result: 2

// Result: 0.01

// Result: 4.01

### Math.random()

You will often find your self in a situation where you need to produce something random and usually that starts with generating a random number. We can do this in JavaScript using the random() method found on the Math object. This method always generate a random, floating point number between 0 (inclusive) and 1 (exclusive). Here's an example:

``````// Example output: 0.5611124673777659

But what if you need this number to be a positive integer? Well, we can just multiply it with a number and then round it, like this:

``````// A number between 0 and 9

// A number between 1 and 10

I multiply by 10, to create a number between 0 and 10, but notice how I vary between the ceil() and floor() methods, to control whether 0 and 10 are included.

Since the number we get from the random() method is between 0 and ~0.99999999, the fact that we either round up or down to the nearest integer will make either 0 or 10 exclusive when we use the ceil() and floor() method.

But what if we need to generate a number that falls between an arbitrary range, e.g. between 1 and 255? Well, with a bit of clever math, we can accomplish this quite easily. Here's an example, where I have created a function that takes a minimum and a maximum parameter, to easily generate a number within a specific range:

``````function GetRandomNumber(min, max)
{
return Math.random() * (max - min) + min;
}

// Example output: 163.11894979340966

// Example output: 163

By default, our function returns a floating point number, but as you can see from the last line of the example, we can easily round this to an integer.

### Math.round()

We previously used the ceil() method to round up to the nearest integer, and the floor() method to round down to the nearest integer.

However, in many situations, you may want to let the number in question decide whether it should be rounded up or down. For instance, 6.49 should be rounded down to 6, while 6.51 should be rounded up to 7. Fortunately, we have the Math.round() method for just that:

``````// Result: 1

// Result: 10

// Result: 10

// Result: 42

As you can see from the last example, JavaScript will round up if the value is positive and exactly between the two nearest integers.

### Math.trunc()

We have already used three different methods for rounding numbers (ceil(), floor() and round()), but JavaScript actually comes with a fourth variant for doing this: The trunc() method. It's actually a bit more simple than the methods we have already seen, because it works by simply removing the fractional part of a floating point number, only leaving the integer part. Here's a couple of examples that will illustrate what it does:

``````// Result: 0

// Result: 10