The JavaScript Math object allows you to perform mathematical tasks on numbers. Unlike other objects, the Math object has no constructor.
The Math object is static. This means you don't need to create a Math object first using the new keyword to use it. All its properties and methods can be accessed directly using Math.propertyName or Math.methodName().
The syntax for any Math property is Math.property. JavaScript provides 8 mathematical constants that can be accessed directly from the Math object.
| Property | Description |
|---|---|
Math.E |
Returns Euler's number (approx. 2.718) |
Math.PI |
Returns PI (approx. 3.14159) |
Math.SQRT2 |
Returns the square root of 2 (approx. 1.414) |
Math.SQRT1_2 |
Returns the square root of 1/2 (approx. 0.707) |
Math.LN2 |
Returns the natural logarithm of 2 (approx. 0.693) |
Math.LN10 |
Returns the natural logarithm of 10 (approx. 2.302) |
Math.LOG2E |
Returns base 2 logarithm of E (approx. 1.442) |
Math.LOG10E |
Returns base 10 logarithm of E (approx. 0.434) |
console.log("PI is: " + Math.PI);
console.log("Euler's number is: " + Math.E);
JavaScript provides four common methods to round a number to an integer:
Math.round(x): Returns x rounded to its nearest integer.Math.ceil(x): Returns x rounded up to its nearest integer.Math.floor(x): Returns x rounded down to its nearest integer.Math.trunc(x): Returns the integer part of x (removes the decimals).console.log(Math.round(4.6)); // 5 (Rounds to nearest) console.log(Math.ceil(4.2)); // 5 (Always rounds UP) console.log(Math.floor(4.9)); // 4 (Always rounds DOWN) console.log(Math.trunc(4.9)); // 4 (Just chops off decimals)
Math.sign(x)The Math.sign() method returns -1 if the number is negative, 1 if it is positive, and 0 if it is zero.
Math.abs(x)The Math.abs() method returns the absolute (positive) value of x. It effectively removes the minus sign from negative numbers.
console.log(Math.sign(-40)); // -1 console.log(Math.sign(50)); // 1console.log(Math.abs(-4.7)); // 4.7
Math.pow(x, y)The Math.pow(x, y) method returns the value of x to the power of y ($x^y$).
Math.sqrt(x)The Math.sqrt(x) method returns the square root of x.
console.log(Math.pow(8, 2)); // 64 (8 * 8) console.log(Math.sqrt(64)); // 8
The Math.min() and Math.max() methods can be used to find the lowest or highest value in a list of arguments.
// Find the lowest number let min = Math.min(0, 150, 30, 20, -8, -200);// Find the highest number let max = Math.max(0, 150, 30, 20, -8, -200);
console.log(min); // -200 console.log(max); // 150
Pro Tip: To find the minimum or maximum value in an Array, you can use the spread operator (
...)! Example:Math.max(...myArray)
The Math object also provides trigonometric functions, which are very useful when building games or doing complex geometry:
Math.sin(x), Math.cos(x), Math.tan(x): Return the sine, cosine, and tangent of an angle (provided in radians, not degrees).radians = degrees * Math.PI / 180// Calculate the sine of 90 degrees let degrees = 90; let radians = degrees * Math.PI / 180; console.log(Math.sin(radians)); // Outputs: 1
Which Math method always rounds a number up to the nearest integer?