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 dot pi very often or math dot E for Oilers 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 Asian or finding absolute value one that will really use a lot is actually
random numbers which will 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
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 upper case m if we tried 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 dots 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 dot Pi 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 space ship or something.
But some of the more useful methods in here include things like math dot floor which expects us to pass
in a number like three point six five seven and it while I use a comma there.
But it also works.
It's going to chop off the decimal point so it doesn't round.
I could have three point nine 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 dot round and math out round does round.
So if I have math that round a four point six we get five but four point four is rounded down to four.
We also have math that POW math dot power where we can pass in two numbers like 7 and 2 and this will
take 7 and raise it to the second power or I could do 7 to the 7th 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 arcs all this stuff we've got
some geometry getting the hypotenuse of a triangle we 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 dot random.
So math dot 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 dot random.
We don't pass anything into those parentheses what it does is it returns a Random decimal between 0
and 1 but not including 1.
So if we want a random number from 1 to 10 for example we have to transform it in a couple 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
1.
Then we multiply it by 10.
So now we have a random number with a decimal going from zero all the way up to nine point 9 9 9 9 9
9 9 9 9 9 9.
Then what we do is we floor it which we just saw a math top floor 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 1.
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 one to six 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 could went all the way up to five point nine.
It's theoretically possible to go to five point 9 9 9 9 9 9 9 and so on.
Then what we do after that is math dot floor the whole thing math dot floor which chops off a decimal
point it doesn't round it just removes that decimal.
So now we end up with a number from zero to 5.
So I just got five and eventually I'll get zero.
There we go zero to five.
Which I could use for a dice roll but most dice go from one to six.
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 in to two numbers.
I want a random integer from 50 to 100 in JavaScript.
You have to do that yourself.
The only built in way of getting a random number is math at random which always gives us a decimal from
0 to 1.