Skip to content
Home Β» The Math Object

The Math Object

🌐 The Math Object in JavaScript


🧠 1. Introduction to Math Object

The Math Object in JavaScript provides mathematical constants and functions for performing calculations.

πŸ‘‰ It is a built-in object and not a constructor, so it is used directly without creating an instance.

βœ… Syntax:

Math.methodName(value);

πŸ” 2. Characteristics of Math Object

  • Static object (no need to create object)
  • Contains constants and methods
  • Used for numeric calculations
  • Does not work with complex numbers

πŸ”’ 3. Math Constants

ConstantDescriptionExample
Math.PIValue of Ο€3.14159
Math.EEuler’s number2.718
Math.SQRT2√21.414
Math.SQRT1_2√(1/2)0.707

πŸ’» Example:

console.log(Math.PI);
console.log(Math.E);

βš™οΈ 4. Important Math Methods


πŸ”Ό (1) Math.round()

Rounds to nearest integer

console.log(Math.round(4.6)); // 5
console.log(Math.round(4.3)); // 4

πŸ”½ (2) Math.floor()

Rounds down

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

πŸ”Ό (3) Math.ceil()

Rounds up

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

βœ‚οΈ (4) Math.trunc()

Removes decimal part

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

πŸ”’ (5) Math.pow()

Power calculation

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

√ (6) Math.sqrt()

Square root

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

🎲 (7) Math.random()

Generates random number (0 to 1)

console.log(Math.random());

🎯 Random Number Example:

let num = Math.floor(Math.random() * 10);
console.log(num); // 0–9

πŸ” (8) Math.max()

Returns largest value

console.log(Math.max(10, 20, 30)); // 30

πŸ”½ (9) Math.min()

Returns smallest value

console.log(Math.min(10, 20, 30)); // 10

βž•βž– (10) Math.abs()

Absolute value

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

πŸ”’ (11) Math.log()

Natural logarithm

console.log(Math.log(1)); // 0

πŸ”’ (12) Math.sin(), Math.cos(), Math.tan()

Trigonometric functions

console.log(Math.sin(0)); // 0
console.log(Math.cos(0)); // 1

πŸ”„ 5. Complete Example

let num = 5.7;

console.log("Round:", Math.round(num));
console.log("Floor:", Math.floor(num));
console.log("Ceil:", Math.ceil(num));
console.log("Power:", Math.pow(2, 3));
console.log("Square Root:", Math.sqrt(25));
console.log("Random:", Math.random());
console.log("Max:", Math.max(10, 50, 30));
console.log("Min:", Math.min(10, 50, 30));

πŸ”„ 6. Summary Table

MethodPurpose
round()Nearest integer
floor()Round down
ceil()Round up
trunc()Remove decimal
pow()Power
sqrt()Square root
random()Random number
max()Largest value
min()Smallest value
abs()Absolute value

⚠️ 7. Important Notes

  • Math object is static
  • No need to use new Math()
  • Used widely in:
    • Games (random numbers)
    • Calculations
    • Data analysis

🎯 Conclusion

The Math Object is essential for:

  • Performing mathematical operations
  • Generating random values
  • Implementing logic-based applications

πŸ‘‰ It is widely used in web applications, games, and data processing.