In JavaScript, in addition to the methods available to modify objects and the possibility of creating our own methods using the prototype, We have a series of predefined resources that allow us to execute various actions. Among these resources is the library or object math, which in turn contains a series of functions. In this post, we will teach you what it is and how the object works math in JavaScript for you to use in your own projects.
What is the math object in JavaScript?
The object math In JavaScript it is a type of module that comes preloaded in this programming language. That is to say, is a global object that we have access to without needing to insert a require.
As you can read in our post about JavaScript objects, they are characterized by having a series of functions available that allow us to modify their properties. The functions that are part of an object are known as methods.
Then the object math JavaScript has a series of methods available that we can use to manipulate elements. To use this element, we must insert the command object.property, with which we can access the properties of an object. Keep in mind that this object cannot be modified. That is to say, all its properties and methods are static.
The name of this object may give you some idea of its use, since Its properties and methods are specific to constants and mathematical functions. One of the most used methods of this object is Math.randomwhich you may have seen if you created the method shuffle in JavaScript, since we use it in its function to modify the prototype of a array.
How does JavaScript’s math.random method work?
The method random of the object math in JavaScript It allows us to return random numbers in a function. To do this, we can use several operations, in which we include minimum and maximum values. Thus, The random number that the method will return will be found between these values.
Keep in mind that the method random in itself it accepts no value. Thus, we will always find it written as Math.random(), without any value between its parentheses. When we use this method alone, that is, without an operation, we will obtain a random number between 0 and 1. Therefore, when we want this scale to change, we can use one of the JavaScript arithmetic operators to modify it.
We give you an example, suppose we want to obtain a random number between 0 and 10. Then, we can multiply the method math.random for 10. Here’s how we would write this into a function to get a random number of goals:
generateGoals ( ) {
return Math.random ( )* 10
}
In addition to having a scale between 0 and 1, this method of the object math In JavaScript it returns a floating point number. That is, it can return us, for example, the number 0.56712. If we want to convert this value to an integer, as can happen in the case of goals in a soccer match, we will have to use another method of the object math: Math.floor. This method allows us to always obtain an integer.
So the function using the two methods would look like this:
generateGoals ( ) {
return Math.floor(Math.random ( ) * 10);
}
This will make our project goals go from 0 to 9 (the maximum value is not included in the limit).
Whats Next?
Although now you know what the object is math in JavaScript and how it works, Surely you still have many doubts about this complex programming language. Therefore, we invite you to answer all your questions in our Full Stack Web Development Bootcamp. There, you will not only learn to master JavaScript, but also other programming languages for the web, such as CSS and HTML. What are you waiting for to start? Sign up now!