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) -
Math.E
: The base of the natural logarithm (approx. 2.71828). -
Math.LN2
: The natural logarithm of 2 (approx. 0.693). -
Math.LN10
: The natural logarithm of 10 (approx. 2.302). -
Math.LOG2E
: The base 2 logarithm of E (approx. 1.442). -
Math.LOG10E
: The base 10 logarithm of E (approx. 0.434). -
Math.SQRT2
: The square root of 2 (approx. 1.414). -
Math.SQRT1_2
: The square root of 1/2 (approx. 0.707). -
Math.TAU
: The constant 2π, useful for full rotations in radians (approx. 6.283).
2️⃣ Mathematical Methods
Here are some of the most commonly used Math
methods:
2.1 Rounding Methods
-
Math.round(x)
: Returns the value ofx
rounded to the nearest integer. -
Math.ceil(x)
: Returns the smallest integer greater than or equal tox
(rounds up). -
Math.floor(x)
: Returns the largest integer less than or equal tox
(rounds down). -
Math.trunc(x)
: Returns the integer part ofx
(removes the fractional part).
2.2 Absolute Value
-
Math.abs(x)
: Returns the absolute (positive) value ofx
.
2.3 Power and Exponentiation
-
Math.pow(base, exponent)
: Returns the base raised to the power of the exponent. -
Math.sqrt(x)
: Returns the square root ofx
. -
Math.cbrt(x)
: Returns the cube root ofx
. -
Math.exp(x)
: Returnse
raised to the power ofx
(e^x
).
2.4 Logarithmic Methods
-
Math.log(x)
: Returns the natural logarithm (basee
) ofx
. -
Math.log10(x)
: Returns the base-10 logarithm ofx
. -
Math.log2(x)
: Returns the base-2 logarithm ofx
.
2.5 Trigonometric Methods
-
Math.sin(x)
: Returns the sine ofx
(x in radians). -
Math.cos(x)
: Returns the cosine ofx
(x in radians). -
Math.tan(x)
: Returns the tangent ofx
(x in radians). -
Math.asin(x)
: Returns the arcsine ofx
in radians. -
Math.acos(x)
: Returns the arccosine ofx
in radians. -
Math.atan(x)
: Returns the arctangent ofx
in radians.
2.6 Random Number Generation
-
Math.random()
: Returns a random floating-point number between0
(inclusive) and1
(exclusive). -
Math.random()
can be used to generate random numbers in a specific range. For example, to generate a random integer betweenmin
andmax
(inclusive):
2.7 Maximum and Minimum Values
-
Math.max(a, b, c, ...)
: Returns the largest of zero or more numbers. -
Math.min(a, b, c, ...)
: Returns the smallest of zero or more numbers.
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! 😊