Next up, a one off video on something called the math object built into JavaScript, out of the box
for free.
We get access to this thing called math with a capital M, and it's just a collection of properties
and methods that have to do with math in some way.
So we have methods like rounding or round.
We have a method called ABB's for absolute value.
We have constants, mathematical constants like PI, the value of PI three point one four one five nine
and so on, which at one point I knew the first two digits of in like fourth fifth grade.
We had a competition at my school anyway.
I've lost almost all of that.
So this math object is here in the console.
I don't have to do anything.
I can just reference it and I can actually just hit enter on math and expand this.
So we haven't talked about objects yet.
And you don't need to really understand what an object is in JavaScript, except for the fact that it's
a collection of properties and methods.
So one option would have been to make just a variable called PI that we always have access to.
And another one called E Foo.
No Euler's, no.
Or, you know, the square root of two that could just be available anywhere.
Right.
So I could just do that or JavaScript could do that.
It doesn't, because instead of just polluting the namespace, which basically means instead of just
having a bunch of variable names taken up, JavaScript approach was to combine all of those into a single
object.
So they're all nested under this thing called math.
So I have to access math, dot pi or math dot e for euler's.
No, you probably won't really need either of these, at least not with what we're going to focus on
for now.
But they are there.
But what I want to focus on are the methods.
These methods range from things that you probably won't need to use at all.
Like, I don't know Artan 10 or what is its cosine sine square root you may want to know about.
But the ones I'll be showing are mainly around random numbers and a little bit around rounding and flooring
your numbers so you can read more about the math objects on MDN.
There's a page for every single method.
If you wanted to learn about sine and how it works, not necessarily the math behind it, but how the
sine method works and if it expects any arguments and how they work and all that.
But I'll just show a couple of these methods right off the bat.
First of all, we've got one called floor math.
The floor will take a number with a decimal and chop off that decimal.
So it's not rounding.
This could be nine nine nine nine nine nine nine, which would round up to twenty four.
It's just chopping it off.
So that's floor.
We also have ceiling or seal, so I could have thirty four point one and it's going to rounded up no
matter what.
So thirty four point nine nine nine nine is going to seal or ceiling to three to thirty five and thirty
four point zero one will also round up ceiling up to thirty five.
So those maybe not that useful on their own.
But what I'm going to show you now is how we generate random numbers.
So there's a built in method to that math object.
And this is really the whole reason I'm showing you the math objects in the first place.
There's a built in method called random and random is going to give us a random decimal number between
zero and one, but it won't actually include one.
So it's going to be between zero and, you know, nine point nine nine nine nine nine nine nine nine
nine math dot random just like that.
So every time I call that it is a method.
I need those parentheses.
Every time I call it, I get a different very long decimal usually.
Well, sometimes that's fine.
But most of the time if you're trying to get a random number, we might be trying to get a random number
between one and two.
We might want a coin flip or zero and one where it's just integers.
The whole number one.
Whole number two, what's your favorite number between one and 100?
I don't want to hear, you know, ninety four point one two eight one five nine three four.
I just want to hear ninety one or ninety two, whatever the number is.
So to do that requires a bit more work.
We don't actually have a method called random integer or random int or something like that.
Many other programming languages do in JavaScript.
We don't, but we can do some math, we can take a number like this and then Floret in order to get
a whole number.
So it's a multi-step process.
Let's say we want random numbers between one and ten.
I can call math at random.
So that gives me a very long decimal between zero and one.
Then I can multiply by ten.
So now I'm looking at five point nine, six one, blah, blah, blah.
Then I can floor that to just get five.
OK, so let's try that, let's do a number between one and five, so math.
Not random, and I'm going to multiply by five, I want a number between one and five, so that gives
me three point seven this time.
Next time it's two point no, that can be one point eight four point one four point two zero point eight.
OK, and then I'm going to math, not for that, but I want to for the entire thing.
So what I did here was put it in a variable and then for that.
But that's a bit extra.
I don't really need this.
I don't need to step on its own.
So I'm going to do a math flaw, remember?
What floor does it chops off the decimal?
So math that floor of our random number multiplied by five.
OK, so we're getting zero three four, three, four two one.
So I am getting five random numbers.
The problem I have now is that it's going from zero.
Right?
Our random numbers, just math that random in general goes from zero to one non-inclusive.
Then I multiply it by five.
I'm never actually going to have five as a random number.
I'm going to have zero up until four.
So if I want one to five, I then need to shift everything up by adding one.
So this will give me a random number from one to five.
Now, what if I wanted to go from one to ten?
Well, I just multiply this by 10.
Now, I have random numbers between one in 10 random integers.
What if I want to go from 20 to 22?
Well, all I need to do is figure out how many options, how many numbers am I trying to generate?
Well, 20, 21 and 22.
That's three numbers.
And then I can shift this upwards by 20.
So I get twenty twenty one.
Twenty two.
So not that common to do that.
It's most, I'd say most common to generate a number starting from either zero to something or one to
something.
But if you ever need a range, you can shift it upwards as much as needed.
So that is math at random on its own.
It gives us a random decimal from zero to one.
We can transform that and use math up floor to help us get integers where we don't have that crazy look
in decimal.
So we have other methods on the math object right round absolute value power, which will raise something
to a power.
Although we already know how to do that.
We don't really need the math object.
Right.
We can do this.
Math, power of, say, two to the third is the same as just two star star three.
So if you have any other questions about the math object, take a look at MDN.
Remember, we have not really covered objects at all.
So all that we know right now is that an object is just a place to store stuff.
So it's not all hanging out on its own in the middle of nowhere.
All this math functionality has been combined into a single object, a single entity called Math with
a capital M.