The use of the Math object in JS

Source: Internet
Author: User
Tags natural logarithm square root

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.