JavaScript Math object Cheat Sheet that you cannot miss

JavaScript Math object Cheat Sheet that you cannot miss

Javascript has handy built-in objects. One of these popular built-in objects is the Math Object.

  1. Number Constants

    Here are some of the built-in number constants that exist on the Math object:

    The Pi number: Math.PI, which is approximately 3.1459

    The Euler's constant: Math.E, approximately 2.718

    The natural Logarithm of 2: Math.LN2, approximately 0.693 in value

  2. Rounding methods

    These include:

    Math.ceil(): rounds up to the closest integer

    Math.floor(): rounds down to the closest integer

    Math.round(): rounds up to the closest integer if the decimal is 0.5 or above, otherwise rounds down to the closest integer

    Math.trunc(): trims the decimal, leaving only the integer

  3. Arithmetic and Calculus methods

    Here is a non-conclusive list of some commonly used arithmetic and calculus methods that exist for the math object:

    Math.pow(2,3): calculates the number 2 to the power 3, resulting in 8

    Math.sqrt(16): calculates the square root of the number 16, resulting in 4

    Math.cbrt(27): calculates the cube root of the number 27, resulting in 3

    Math.abs(-10): returns the absolute value(always positive), resulting in 10

  4. Logarithmic methods: Math.log(), Math.log2(), Math.log10()

  5. Return the minimum and maximum values of all the inputs: Math.min(9,8,7) returns 7, Math.max(9,8,7) returns 9.

  6. Trigonometric methods: Maths.sin(), Maths.cos(), Math.tan(), etc


Thats all for this blog, hope it was worth your time. Thank you.