JS Math

JavaScript Math Object: A Beginner's Guide

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().


1. Math Properties (Constants)

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)

Math Constants Example

console.log("PI is: " + Math.PI);
console.log("Euler's number is: " + Math.E);

2. Math Rounding Methods

JavaScript provides four common methods to round a number to an integer:

Math Rounding Example

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)

3. Math Sign and Absolute Values

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.

Math Sign and Abs Example

console.log(Math.sign(-40)); // -1
console.log(Math.sign(50));  // 1

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


4. Power and Square Root

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.

Math Pow and Sqrt Example

console.log(Math.pow(8, 2)); // 64 (8 * 8)
console.log(Math.sqrt(64));  // 8

5. Finding Minimum and Maximum Values

The Math.min() and Math.max() methods can be used to find the lowest or highest value in a list of arguments.

Math Min and Max Example

// 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)


6. Trigonometry (Advanced)

The Math object also provides trigonometric functions, which are very useful when building games or doing complex geometry:

// Calculate the sine of 90 degrees
let degrees = 90;
let radians = degrees * Math.PI / 180;
console.log(Math.sin(radians)); // Outputs: 1

Exercise

?

Which Math method always rounds a number up to the nearest integer?