JavaScript Math

Why Use the Math Object?

The Math object is essential for tasks like:

  • Calculating square roots, exponents and logarithms.
  • Generating random numbers.
  • Rounding values up or down.
  • Performing trigonometric calculations.

Whether you’re working on animations, gaming logic or complex algorithms, the Math object simplifies calculations.

Key Properties of the Math Object

The Math object includes several constants that represent important mathematical values:

PropertyValueDescription
Math.PI3.141592653589793The ratio of a circle’s circumference to its diameter.
Math.E2.718281828459045Euler’s number, the base of natural logarithms.
Math.LN20.6931471805599453Natural logarithm of 2.
Math.LN102.302585092994046Natural logarithm of 10.
Math.SQRT21.4142135623730951Square root of 2.
Math.SQRT1_20.7071067811865476Square root of 1/2.

Example:

console.log(Math.PI);   // Output: 3.141592653589793
console.log(Math.SQRT2); // Output: 1.4142135623730951

Common Methods of the Math Object

1. Rounding Methods

MethodDescriptionExample
Math.round()Rounds a number to the nearest integer.Math.round(4.7) → 5
Math.ceil()Rounds a number up to the next integer.Math.ceil(4.1) → 5
Math.floor()Rounds a number down to the nearest integer.Math.floor(4.9) → 4
Math.trunc()Truncates the decimal part of a number.Math.trunc(4.9) → 4

Examples:

console.log(Math.round(4.6)); // Output: 5
console.log(Math.ceil(4.2)); // Output: 5
console.log(Math.floor(4.8)); // Output: 4
console.log(Math.trunc(4.9)); // Output: 4

2. Math.random()

Generates a random number between 0 (inclusive) and 1 (exclusive).

Example:

const randomNum = Math.random();
console.log(randomNum); // Output: Random number like 0.347890123

// Generate a random number between 1 and 10
const random1to10 = Math.floor(Math.random() * 10) + 1;
console.log(random1to10); // Output: Random integer between 1 and 10

3. Exponents and Roots

MethodDescriptionExample
Math.pow(x, y)Raises x to the power of y.Math.pow(2, 3) → 8
Math.sqrt(x)Returns the square root of x.Math.sqrt(16) → 4
Math.cbrt(x)Returns the cube root of x.Math.cbrt(27) → 3

Examples:

console.log(Math.pow(2, 3));  // Output: 8
console.log(Math.sqrt(16)); // Output: 4
console.log(Math.cbrt(27)); // Output: 3

4. Trigonometric Methods

MethodDescriptionExample
Math.sin(x)Returns the sine of x (in radians).Math.sin(Math.PI / 2) → 1
Math.cos(x)Returns the cosine of x (in radians).Math.cos(0) → 1
Math.tan(x)Returns the tangent of x (in radians).Math.tan(Math.PI / 4) → 1

Convert degrees to radians for input:

const degrees = 45;
const radians = degrees * (Math.PI / 180);
console.log(Math.sin(radians)); // Output: 0.7071067811865475

5. Logarithmic Methods

MethodDescriptionExample
Math.log(x)Returns the natural logarithm of x.Math.log(Math.E) → 1
Math.log10(x)Returns the base-10 logarithm of x.Math.log10(100) → 2
Math.log2(x)Returns the base-2 logarithm of x.Math.log2(8) → 3

6. Math.max() and Math.min()

MethodDescriptionExample
Math.max(a, b, …)Returns the largest value in a list of numbers.Math.max(10, 20, 5) → 20
Math.min(a, b, …)Returns the smallest value in a list of numbers.Math.min(10, 20, 5) → 5

Examples:

console.log(Math.max(1, 3, 7, 0)); // Output: 7
console.log(Math.min(1, 3, 7, 0)); // Output: 0

Practical Example: Random Dice Roll

function rollDice() {
return Math.floor(Math.random() * 6) + 1; // Random number between 1 and 6
}
console.log(rollDice()); // Output: Random integer like 3

Leave a Comment