Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
JavaScript provides a library of constants and functions that are useful when doing mathematical operations in JavaScript. These items are stored in an object named Math, and in this video we will take a look at several of these functions.

0:00
[? Music playing ?] [Treehouse]

0:08
JavaScript provides us with a wide variety of functions

0:10
and constants that are useful when doing math.

0:13
These are stored in the Math Object, so let's take a look.

0:17
So we've seen the different ways of creating numbers and comparing numbers,

0:20
but there are a few more things that we would want to do

0:22
with numbers that aren't able to be done with just the simple operations that we have here.

0:28
For instance, rounding numbers, getting random numbers, doing square roots, or powers,

0:35
or, for instance, maybe operations like sine and cosine,

0:38
the trigonomic functions that we would want to use

0:40
for instance if we're doing some sort of geometry.

0:43
Fortunately, JavaScript provides an object called Math,

0:47
and that's Math with a capital M.

0:50
And there are different properties and methods on this that relate to common mathematical operations.

0:56
Let's say we want to create a random number.

0:59
So we can create a variable called U,

1:03
and to create a random number, we use our Math Object.

1:05
So, that's capital Math,

1:08
and then there are either properties or methods that we want to call on it.

1:12
To get a random number, we want to do the random method.

1:16
So do Math.random, and then use parentheses to call it.

1:22
So if we refresh, go to our page, and we evaluate U,

1:28
which is the number we just assigned to Math.random's value,

1:33
we'll see we get a number 0.407 blah blah blah,

1:38
and this is because Math.random returns a value between 0 and 1.

1:45
Now this may not be exactly what you want.

1:47
You may have been wanting a number 0 through 10.

1:51
Well, here's how we can do that.

1:53
Because Math.random will return a value between 0 and 1,

1:57
the upper bounds is obviously 1,

1:59
but if we wanted a number 0 through 10, our upper bounds for that range would be 10.

2:06
So an easy way of doing that would be multiplying it by 10.

2:11
So now if we refresh, we should now get a number between 0 and 10.

2:17
We get 9.2.

2:19
Again, every time we refresh it, we're going to get a different number

2:22
because random, every time it is called, will give us a different number.

2:25
So if we keep saving and refreshing, we should get, hopefully, numbers between 0 and 10.

2:31
Now, obviously the numbers that we get here are between 0 and 10,

2:34
but they're not whole numbers.

2:36
So what could we do? Well we could round them.

2:39
So let's take this whole expression here, our Math.random times 10,

2:44
and let's call another method that's on the Math Object called round.

2:51
So what round will do is it will take a number and round it to the nearest whole number.

2:56
So, again, what this will do is it will round it to the nearest number.

3:01
So now we get 1, in this case, but we should always get a whole number between 0 and 10.

3:08
So let's take a look at what round does.

3:10
So if we take a variable V and do Math.round(2.3), V should be 2,

3:23
and if we do 2.7, round should round up to 3.

3:28
And then if we do 2.5, it will round up to 3 as well.

3:33
So Math will round up from .5 and down for anything below .5.

3:39
Now, what if you always want to round down?

3:45
We can do Math.floor,

3:49
and if we put in 3.7, W should always be 3.

3:55
So it will always round down.

3:58
If we want to do the opposite and always round up, we'll use Math.ceil.

4:02
And that's ceil, which is short for ceiling.

4:06
And if we did, for instance, 6.2, Math.ceil of 6.2 would be 7.

4:13
Now there are a couple of other methods.

4:15
For instance, if you wanted to take the power of something, we could do var Y = Math.pow,

4:24
and this takes 2 arguments, the base and the exponent.

4:26
So if you wanted to take 2 to the power of 5, we'll do Math.pow(2,5).

4:35
And Y would be 32.

4:39
And we also have the square root method.

4:41
So we'll take Z=Math.sqrt for square root.

4:47
So this just takes 1 argument. So say the square root of 4. Z should be 2.

4:53
And this will work for things that won't square root so easily like 5.

4:58
You get 2.2, or for instance, the square root of 2 will 1.414.

5:05
Now there are quite a few different things in the Math Object

5:07
that you may want to use at certain points in your programs.

5:11
You'll know them when you need them, for instance, sine or cosine.

5:14
But we can take a look at what our options are just by typing in Math into the console here

5:20
and expanding it out.

5:23
You can see we have things like floor, ceil, rand, pow,

5:27
things like min and max, which are nice.

5:30
For instance, if we take Math.min and pass in 2 and 4, it will return us the minimum,

5:38
and similarly maximum will return the maximum.

5:42
And you can add as many arguments as you like to Math.min or max.

5:47
So anytime you need the min or max, you can get those.

5:50
Sine, cosine, and tangent are available in sine, cos, and tan.

5:54
As well as arc sine, arc tan, and arc cos in acos, asine, and atan.

6:01
Again, depending on what you need to do, you may never use these,

6:04
but if you're doing some interesting geometry, obviously these will come in handy.

6:07
Also, we have abs, which will return the absolute value.

6:11
So, Math.abs of 1 would be 1.

6:19
Finally, you'll see up here we have some things that aren't methods,

6:23
but instead they are constants or simply properties of the Math Object.

6:28
So if you need a pretty decent value of pi, you can do Math.pi.

6:34
So Math.pi, which is 3.14159 down a few decimal places.

6:43
Or if you need e or if you need the natural log of 10 or the value of e or the square root of 2,

6:50
these are all constants that are stored in the Math Object.

6:54
Anytime you need to refer to it, I recommend just taking a look at Math in your console,

6:58
and it will give you the list of constants and functions

7:03
that will be commonly used in mathematical operations.

7:06
[? Music playing ?] So now you should be familiar with all the tools you'll need

7:09
to use when doing math in JavaScript.

7:12
[? Music playing ?] [Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up