📝 In this article, you will learn:

  • What the Math object is in JavaScript
  • Common Math methods and their usage
  • How to perform calculations, rounding, and generate random numbers
  • Examples of real-world math operations in JavaScript

🔍 What Is the Math Object?

JavaScript provides a built-in Math object for performing mathematical tasks. It includes properties for constants (like Math.PI) and methods for common calculations (like rounding, powers, and trigonometry).

Note: The Math object is not a constructor, so you don’t need to use new Math() — just use Math directly.

🧮 Common Math Properties

1️⃣ Math.PI

Represents the value of π (pi):

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

2️⃣ Math.E

Euler’s number (approx. 2.718):

console.log(Math.E);  // Output: 2.718281828459045

🛠️ Useful Math Methods

🔢 Math.round()

Rounds a number to the nearest integer:

console.log(Math.round(4.6));  // Output: 5
console.log(Math.round(4.4));  // Output: 4

⬆️ Math.ceil()

Rounds a number up to the next largest integer:

console.log(Math.ceil(4.1));  // Output: 5

⬇️ Math.floor()

Rounds a number down to the next smallest integer:

console.log(Math.floor(4.9));  // Output: 4

Math.trunc()

Removes the decimal part of a number:

console.log(Math.trunc(4.9));  // Output: 4

🔄 Random Numbers

🎲 Math.random()

Returns a random decimal between 0 (inclusive) and 1 (exclusive):

console.log(Math.random());  // Output: e.g., 0.735323624

To get a random number between 1 and 10:

let random = Math.floor(Math.random() * 10) + 1;
console.log(random);  // Output: A number from 1 to 10

🔢 Powers and Roots

Math.pow()

Raises a number to the power of another:

console.log(Math.pow(2, 3));  // Output: 8 (2^3)

Math.sqrt()

Returns the square root:

console.log(Math.sqrt(16));  // Output: 4

📉 Min and Max

Math.max()

Returns the highest value among arguments:

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

Math.min()

Returns the lowest value:

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

🔄 Absolute Value

Math.abs()

Returns the absolute (non-negative) value:

console.log(Math.abs(-5));  // Output: 5

🔁 Other Math Methods

Math.sin(), Math.cos(), Math.tan()

Used for trigonometric calculations (angles are in radians):

console.log(Math.sin(Math.PI / 2));  // Output: 1

Math.log(), Math.log10()

For natural and base-10 logarithms:

console.log(Math.log(Math.E));     // Output: 1
console.log(Math.log10(1000));     // Output: 3

💡 Real-World Examples

  • Random quote generators: Use Math.random() to display a random message.
  • Game scores: Round scores with Math.floor() or Math.round().
  • Chart scales: Use Math.max() and Math.min() to find limits.

🧠 Recap

  • The Math object is built-in and used for advanced numeric calculations.
  • It includes constants like Math.PI and methods like Math.round(), Math.random(), Math.sqrt(), etc.
  • Use Math.floor() and Math.ceil() to control rounding direction.
  • Combine methods for powerful, practical number operations in real apps.