TOC
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)
alert(Math.ceil(4));

// Result: 1
alert(Math.ceil(0.95));

// Result: 7
alert(Math.ceil(6.01));

// Result: 8
alert(Math.ceil(7.95));

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)
alert(Math.floor(4));

// Result: 0
alert(Math.floor(0.95));

// Result: 6
alert(Math.floor(6.01));

// Result: 7
alert(Math.floor(7.95));

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
alert(Math.max(1, 2));

// Result: 8
alert(Math.max(1, 8, 2));

// Result: 0.97
alert(Math.max(0.95, 0.97, 0.01));

// Result: 10.01
alert(Math.max(4, 7.97, 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
alert(Math.min(1, 2));

// Result: 2
alert(Math.min(10, 8, 2));

// Result: 0.01
alert(Math.min(0.95, 0.97, 0.01));

// Result: 4.01
alert(Math.min(4.01, 7.97, 10));

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
alert(Math.random());

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
alert(Math.floor(Math.random() * 10));

// A number between 1 and 10
alert(Math.ceil(Math.random() * 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
alert(GetRandomNumber(0, 255));

// Example output: 163
alert(Math.floor(GetRandomNumber(0, 255)));

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
alert(Math.round(0.51));

// Result: 10
alert(Math.round(10.42));

// Result: 10
alert(Math.round(9.71));

// Result: 42
alert(Math.round(41.50));

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
alert(Math.trunc(0.51));

// Result: 10
alert(Math.trunc(10.42));

// Result: 9
alert(Math.trunc(9.71));

// Result: 41
alert(Math.trunc(41.50));

Summary

Doing basic mathematical operations in JavaScript is quite simple, as we have seen in previous articles. When you need to do more complex operations, the built-in Math object comes in handy, as we have seen in this article. And remember that we haven't covered all available methods in this article - for a full reference, please see the Math object documentation.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!