So in this blog, we are going to understand what is Math function in javaScript, also understand where we should use this
Basically, math function is used for various purposes in javascript some of which are listed below
Math.round()
Math.ceil()
Math.random()
Math.sqrt()
These above are the basic most used Math function in the real word
Let's understand them one by one with examples
Math.round()
So Math.round() is used to get the value in rounded form. means if the value contains a decimal, it will return the integer number. Also, It will go upward and backward flow means it will return the nearest integer of the given decimal Let's see an example
Math.ceil()
So Math.ceil function is used to get the integer number, it is similar to Math.round() but the main difference is it gives a larger number, which is greater than or equal to a given number. Let's see an example
here we can clearly understand the difference in both cases it will return the higher nearest value.
Math.random()
So this is a very commonly used function in a Js production-level environment.
Do you ever think how OTP numbers are generated ???
so the answer is Math.random() function, It will return a random number every time you hit the server. Let's see an example :
Here we can see that for the same line of code, We got a different output.
Math.sqrt()
As the name suggests it will return a square root of any number.
Let's see an example
That's all for the Math functions in JS.
If you have any query, feel free to comment.
Happy Learning:)