Next up, let's talk about something called the math object.
This is a relatively quick topic.
Definitely not long enough to merit its own section.
And it kind of has to do with the stuff we've been talking about, primitives and numbers.
The math object is a collection of different pieces of mathematical functionality, along with some
mathematical constants.
So you probably won't be using math pie very often or math E for Euler's number, but they are available
to you.
Most of what we use or what I use the math object for are its methods.
So just like strings, we have some common methods that we've talked about.
The math object has methods like rounding or exponential action or finding absolute value.
One that will really use a lot is actually random numbers, which we'll come back to in a moment.
Let's start with a quick look at the math object.
We haven't really talked about objects at all in this case.
Think of the math object as just a collection of math functionality, different pieces of or different
actions that have to do with math and numbers in some way.
They're collected into this object called math, and it does start with an uppercase M If we try it
out with a lowercase m, we won't get anything unless we use uppercase.
M Math is just there for us to use.
We have access to it.
And if I type math dot, I can look at the autocomplete in my console and I'll see tons of different
methods, different pieces of functionality.
So something like math pie is not actually a method, it's just a value I can look at.
Probably not that useful most of the time until you get to doing things with maybe geometry or some
sort of game where you have a collision detection radius around your spaceship or something.
But some of the more useful methods in here include things like math flaw, which expects us to pass
in a number like 3.6, five seven.
And it well, I used a comma there, but it also works.
It's going to chop off the decimal point so it doesn't round.
I could have 3.9, nine, nine, nine and a bunch of nines and I still get three as a result.
It just chops that off.
But we also have math round and math out round does round.
So if I have math out round of 4.6, we get five, but 4.4 is rounded down to four.
We also have math, pow, math power where we can pass in two numbers like seven and two, and this
will take seven and raise it to the second power.
Or I could do seven to the seventh power and get a very large number.
Other than that, let's see, is there anything worth bringing up?
We've got tons of methods around trigonometry, cosine and sine tangent, all this stuff.
We've got some geometry getting the hypotenuse of a triangle.
You probably won't use most of those all that often.
But there is one method that we will use a lot, and it's really the main reason, the motivation for
me, even showing you the math object and that method is called math random.
So math at random is going to be used to generate random numbers for us, which we do pretty frequently.
Depending on what you're actually doing in an application.
You may not generate random numbers if you're creating a chat app, but in games or animations, for
example, randomness can be important.
When we call math at random, we don't pass anything in to those parentheses.
What it does is it returns a random decimal between zero and one, but not including one.
So if we want a random number from 1 to 10, for example, we have to transform it in a couple of different
ways.
We have to work it into the form we want.
We use this random decimal as a seed, but then we take advantage of some other methods and some other
operators to get the random number into a workable form.
Here's a step by step breakdown.
Let's say I'm trying to get a random number from 1 to 10.
I start with math at random that gives me a random decimal, as we just saw, from 0 to 1, not including
one.
Then we multiply it by ten.
So now we have a random number with a decimal going from zero all the way up to 9.999999999999.
Then what we do is we floor it.
We we just saw a math flaw.
It chops off the decimal.
So that gives us a random whole number, a random integer from 0 to 9.
And if we want to go from 1 to 10, then we simply add one.
And usually we do this all in one line, which looks like this.
If you're brand new to JavaScript, this looks like a lot, but it's something we do pretty frequently.
Usually with this same pattern, we just change these values here.
So if I was trying to simulate a dice or a die roll from 1 to 6.
I would start with math at random.
Then I would multiply it by six.
So that gives me a random number with a decimal between zero and six, not including six.
As you can see here, we went all the way up to 5.9.
It's theoretically possible to go to 5.999999, nine and so on.
Then what we do after that is math for the whole thing.
Math flaw which chops off the decimal point it doesn't round, it just removes that decimal.
So now we end up with a number from 0 to 5.
So I just got five and eventually I'll get zero.
There we go.
0 to 5, which I could use for a dice roll, but most dice go from 1 to 6.
So I could add one after that.
And now we've got five for one.
And eventually, hopefully, I hit six.
There we go.
So this can take a little bit of getting used to if you have experience with other languages, other
programming languages, some of them will have a method like random integer and you pass it in to two
numbers.
I want a random integer from 50 to 100 and JavaScript.
You have to do that yourself.
The only built in way of getting a random number is math random, which always gives us a decimal from
0 to 1.