Javascript Math

Javascript Math

JavaScript Math Object

The Math Object in JavaScript provides a collection of mathematical functions and constants. It is a built-in object and doesn’t need to be instantiated. You can directly use its properties and methods for performing mathematical operations.


1️⃣ Mathematical Constants

Here are some useful constants available in the Math object:

  • Math.PI: The ratio of the circumference of a circle to its diameter. (approx. 3.14159)

    console.log(Math.PI); // 3.141592653589793
  • Math.E: The base of the natural logarithm (approx. 2.71828).

    console.log(Math.E); // 2.718281828459045
  • Math.LN2: The natural logarithm of 2 (approx. 0.693).

    console.log(Math.LN2); // 0.6931471805599453
  • Math.LN10: The natural logarithm of 10 (approx. 2.302).

    console.log(Math.LN10); // 2.302585092994046
  • Math.LOG2E: The base 2 logarithm of E (approx. 1.442).

    console.log(Math.LOG2E); // 1.4426950408889634
  • Math.LOG10E: The base 10 logarithm of E (approx. 0.434).

    console.log(Math.LOG10E); // 0.4342944819032518
  • Math.SQRT2: The square root of 2 (approx. 1.414).

    console.log(Math.SQRT2); // 1.4142135623730951
  • Math.SQRT1_2: The square root of 1/2 (approx. 0.707).

    console.log(Math.SQRT1_2); // 0.7071067811865476
  • Math.TAU: The constant 2π, useful for full rotations in radians (approx. 6.283).

    console.log(Math.TAU); // 6.283185307179586

2️⃣ Mathematical Methods

Here are some of the most commonly used Math methods:

2.1 Rounding Methods

  • Math.round(x): Returns the value of x rounded to the nearest integer.

    console.log(Math.round(4.5)); // 5 console.log(Math.round(4.4)); // 4
  • Math.ceil(x): Returns the smallest integer greater than or equal to x (rounds up).

    console.log(Math.ceil(4.1)); // 5 console.log(Math.ceil(4.9)); // 5
  • Math.floor(x): Returns the largest integer less than or equal to x (rounds down).

    console.log(Math.floor(4.1)); // 4 console.log(Math.floor(4.9)); // 4
  • Math.trunc(x): Returns the integer part of x (removes the fractional part).

    console.log(Math.trunc(4.9)); // 4 console.log(Math.trunc(-4.9)); // -4

2.2 Absolute Value

  • Math.abs(x): Returns the absolute (positive) value of x.

    console.log(Math.abs(-5)); // 5 console.log(Math.abs(5)); // 5

2.3 Power and Exponentiation

  • Math.pow(base, exponent): Returns the base raised to the power of the exponent.

    console.log(Math.pow(2, 3)); // 8 console.log(Math.pow(5, 2)); // 25
  • Math.sqrt(x): Returns the square root of x.

    console.log(Math.sqrt(16)); // 4 console.log(Math.sqrt(25)); // 5
  • Math.cbrt(x): Returns the cube root of x.

    console.log(Math.cbrt(27)); // 3 console.log(Math.cbrt(8)); // 2
  • Math.exp(x): Returns e raised to the power of x (e^x).

    console.log(Math.exp(1)); // 2.718281828459045

2.4 Logarithmic Methods

  • Math.log(x): Returns the natural logarithm (base e) of x.

    console.log(Math.log(10)); // 2.302585092994046
  • Math.log10(x): Returns the base-10 logarithm of x.

    console.log(Math.log10(100)); // 2
  • Math.log2(x): Returns the base-2 logarithm of x.

    console.log(Math.log2(8)); // 3

2.5 Trigonometric Methods

  • Math.sin(x): Returns the sine of x (x in radians).

    console.log(Math.sin(Math.PI / 2)); // 1
  • Math.cos(x): Returns the cosine of x (x in radians).

    console.log(Math.cos(Math.PI)); // -1
  • Math.tan(x): Returns the tangent of x (x in radians).

    console.log(Math.tan(Math.PI / 4)); // 1
  • Math.asin(x): Returns the arcsine of x in radians.

    console.log(Math.asin(1)); // 1.5707963267948966 (PI / 2)
  • Math.acos(x): Returns the arccosine of x in radians.

    console.log(Math.acos(-1)); // 3.141592653589793 (PI)
  • Math.atan(x): Returns the arctangent of x in radians.

    console.log(Math.atan(1)); // 0.7853981633974483 (PI / 4)

2.6 Random Number Generation

  • Math.random(): Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

    console.log(Math.random()); // A random number between 0 and 1
  • Math.random() can be used to generate random numbers in a specific range. For example, to generate a random integer between min and max (inclusive):

    function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInt(1, 10)); // A random integer between 1 and 10

2.7 Maximum and Minimum Values

  • Math.max(a, b, c, ...): Returns the largest of zero or more numbers.

    console.log(Math.max(1, 5, 2, 9)); // 9
  • Math.min(a, b, c, ...): Returns the smallest of zero or more numbers.

    console.log(Math.min(1, 5, 2, 9)); // 1

3️⃣ Summary

The Math object in JavaScript provides a wide array of methods and constants that allow you to perform various mathematical operations, from basic arithmetic to complex trigonometric and logarithmic calculations. It's a very useful utility for any kind of number manipulation, whether you're working with integers, floating-point numbers, or need to perform complex math tasks.

Let me know if you need more details or examples on any specific Math methods! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close