Math Arithmetic objects: perform ordinary arithmetic tasks;
JavaScript provides 8 arithmetic values that can be accessed by the Math object:
- Constant (MATH.E)
- Pi (Math.PI)
- Square root of 2 (MATH.SQRT2)
- Square root of 1/2 (math.sqrt1_2)
- Natural logarithm of 2 (MATH.LN2)
- Natural logarithm of 10 (MATH.LN10)
- Logarithm of E with base 2 (MATH.LOG2E)
- Logarithm of E with base 10 (math.log10e)
Several common functions are also available in math:
Round () rounding a number
Random () returns an arbitrary number between 0 and 1
Floor () Make a choice of a number (to a small trade-off)
Max () returns the larger of two numbers
MIN () returns the smaller of two numbers
To make a random number 0 to 10, you can use the random () function in math () and the floor () function to
function random () { var random = Math.floor (Math.random () *11); document.getElementById ("demo11"). InnerHTML = random;}
Use the JS above
<id= "demo11"></p>< type = "button" onclick = "random ()" > Show random number </button>
This makes it possible to extract a random number and multiply it by a larger number if you want to use a larger extraction range.
The use of the Math object in JS