The Math object in JavaScript is a built-in object that provides a variety of mathematical functions and constants. It's not a constructor, so it cannot be instantiated. All properties and methods are static and can be accessed directly via the Math object.
Math.PI
:
Description: Returns the value of Pi (π), approximately 3.14159.
Example:
console.log(Math.PI); // Output: 3.141592653589793
Math.E
:
Description: Returns Euler's number, approximately 2.71828.
Example:
console.log(Math.E); // Output: 2.718281828459045
Math.LN2
:
Description: Returns the natural logarithm of 2, approximately 0.693.
Example:
console.log(Math.LN2); // Output: 0.6931471805599453
Math.LN10
:
Description: Returns the natural logarithm of 10, approximately 2.302.
Example:
console.log(Math.LN10); // Output: 2.302585092994046
Math.LOG2E
:
Description: Returns the base-2 logarithm of E, approximately 1.442.
Example:
console.log(Math.LOG2E); // Output: 1.4426950408889634
Math.LOG10E
:
Description: Returns the base-10 logarithm of E, approximately 0.434.
Example:
console.log(Math.LOG10E); // Output: 0.4342944819032518
Math.SQRT2
:
Description: Returns the square root of 2, approximately 1.414.
Example:
console.log(Math.SQRT2); // Output: 1.4142135623730951
Math.SQRT1_2
:
Description: Returns the square root of 1/2, approximately 0.707.
Example:
console.log(Math.SQRT1_2); // Output: 0.7071067811865476
Math.MAX_VALUE
:
Description: Returns the largest possible number in JavaScript.
Example:
console.log(Math.MAX_VALUE); // Output: 1.7976931348623157e+308
Math.MIN_VALUE
:
Description: Returns the smallest positive number in JavaScript.
Example:
console.log(Math.MIN_VALUE); // Output: 5e-324
Math.abs(x)
:
Description: Returns the absolute (positive) value of x
.
Example:
console.log(Math.abs(-5)); // Output: 5
console.log(Math.abs(5)); // Output: 5
Math.ceil(x)
:
Description: Returns the smallest integer greater than or equal to x
.
Example:
console.log(Math.ceil(4.3)); // Output: 5
console.log(Math.ceil(9.01)); // Output: 10