5.11 Math Object

So you've learned a lot about objects, let's now take a look at some of the built-in objects in JavaScript. The first one we're going to look at is the math object. So here on Google, we're going to search for the math object in JavaScript, We're going to go to the first link: Developper.mozilla. org, this is your first reference to learn more about all the objects in JavaScript. So let's take a look at the math object, you can see that this is one of the built-in objects with properties and methods for mathematical constants and functions. So if you scroll down, you can see the properties, for example one of the useful properties is math. PI so that's the number pi. We have a bunch of other properties here, and below that we have all the methods, the first one is absolute, which returns the absolute value of a number, so if you give it a negative number, it returns its positive value. There are quite a few methods here, so I'm not going to go through each one of them, all I want you to know is that we have this math object built into Javascript, so in your applications if you ever need to do math calculations, just go back to this page and see what methods we can use. In this video I'm going to give you a quick demonstration of some of these methods, the first one is the random method. So let's take a look, for each of these methods, we have full documentation with examples. So here, in the console, let's call the random method. So every time we call this method, we get a new random number between 0 and 1. Back to the documentation for the random method, if you scroll down the list of examples, we can see this example. Getting a random number in an interval So basically you need a function like this, which takes a min and a max, and generates a random number between these two values. It's pretty self-explanatory, so you don't need any further explanation. Another useful method Is called math.round, so we can give it 1.9, and it returns 2.. We also have math.max. We can give it a bunch of arguments, and it will return the largest number in the list. Later in the course, this will be one of your exercises. So you're going to write a function like the max method here, which takes a very large number of arguments and returns the largest value. We also have math. min which returns the smallest number in this list. So I'll leave it to you to look at all the methods available in the math object and we'll meet up again for a very next video where we'll talk about strings in JavaScript.