Javascript
Math

JavaScript Math

Provides most of the functions exposed by the Math library (opens in a new tab) in JS.

  1. Math.floor():

    • Returns the largest integer less than or equal to a given number.
    console.log(Math.floor(4.7));  // Output: 4
  2. Math.abs():

    • Returns the absolute value of a number.
    console.log(Math.abs(-4.7));  // Output: 4.7
  3. Math.ceil():

    • Returns the smallest integer greater than or equal to a given number.
    console.log(Math.ceil(4.4));  // Output: 5
  4. Math.round():

    • Rounds a number to the nearest integer.
    console.log(Math.round(4.4));  // Output: 4
  5. Math.trunc():

    • Truncates the decimal part of a number, returning the integer part.
    console.log(Math.trunc(4.7));  // Output: 4
  6. Math.log():

    • Returns the natural logarithm (base e) of a number.
    console.log(Math.log(10));  // Output: 2.302585092994046
  7. Math.pow():

    • Returns the base to the exponent power.
    console.log(Math.pow(2, 3));  // Output: 8
  8. Math.acos():

    • Returns the arccosine (in radians) of a number.
    console.log(Math.acos(0.5));  // Output: 1.0471975511965979
  9. Math.asin():

    • Returns the arcsine (in radians) of a number.
    console.log(Math.asin(0.5));  // Output: 0.5235987755982989
  10. Math.atan():

    • Returns the arctangent (in radians) of a number.
    console.log(Math.atan(1));  // Output: 0.7853981633974483
  11. Math.atan2():

    • Returns the arctangent of the quotient of its arguments.
    console.log(Math.atan2(1, 1));  // Output: 0.7853981633974483
  12. Math.cos():

    • Returns the cosine of a number.
    console.log(Math.cos(0));  // Output: 1
  13. Math.exp():

    • Returns e^x, where x is the argument.
    console.log(Math.exp(1));  // Output: 2.718281828459045
  14. Math.max():

    • Returns the largest of zero or more numbers.
    console.log(Math.max(10, 20));  // Output: 20
  15. Math.min():

    • Returns the smallest of zero or more numbers.
    console.log(Math.min(10, 20));  // Output: 10
  16. Math.sin():

    • Returns the sine of a number.
    console.log(Math.sin(0));  // Output: 0
  17. Math.sqrt():

    • Returns the square root of a number.
    console.log(Math.sqrt(9));  // Output: 3
  18. Math.tan():

    • Returns the tangent of a number.
    console.log(Math.tan(0));  // Output: 0
  19. Math.random():

    • Returns a random number between 0 and 1.
    console.log(Math.random());  // Output: a random number between 0 and 1

Each of these functions can be called using the Math object followed by the function name and the necessary arguments enclosed in parentheses.