Using Math in Javascript
Simple operations with integers
JavaScript has math operators built in. This means that you don't have to import a library just to add some numbers. You can do all the operations as expected.
You can use all basic operations, and can even use addition to join strings together. This isn't the best way to join strings together, but it is commonly used as it is very simple.
Using the Math object
JavaScript has a built-in Math object, which you can use to do more complex mathemactical operators. This object is very commonly used, and can do think like round numbers and pick random numbers from a certain range. In this article, we will only go in depth with picking random numbers, rounding numbers and exponents.
Picking random numbers
It is very useful, especially when something has to be chosen in ranodom, not only numbers. the random function picks a number from 0-1, so if you have more than 2 options you have to multiply it by the number of options.
This is still not ready, as the number could still have a decimal. How do we round it? Well, there are many different ways. You can do the normal rounding, where a integer goes to its closest value(.round), you can round down(.floor) or round up(.celi)
Other math types:
You can use many functions in JavaScript. Some useful ones include: sqrt(number), pow(num,exponent), and math.pi()
Read more about those methods here.