H.dev

The Math Object

Math Made Easy

JavaScript provides a built-in object called Math that allows you to perform mathematical tasks on numbers. Unlike other objects, the Math object has no constructor—you don't use new Math(). All of its properties and methods are static, meaning you just use Math.methodName() directly.

Common Math Methods

1. Math.round(), Math.ceil(), Math.floor() Used for rounding decimal numbers:

console.log(Math.round(4.6)); // 5 (Rounds to nearest integer)
console.log(Math.ceil(4.1));  // 5 (Always rounds UP)
console.log(Math.floor(4.9)); // 4 (Always rounds DOWN)

2. Math.random() Returns a random decimal number between 0 (inclusive) and 1 (exclusive).

let randomDec = Math.random(); 

How to get a random whole number between 1 and 10:

let randomNum = Math.floor(Math.random() * 10) + 1;
console.log(randomNum); 

3. Math.max() and Math.min() Used to find the highest or lowest value in a list of arguments.

let highestScore = Math.max(10, 50, 100, 2);
console.log(highestScore); // 100

(Pro-tip: If you have an array of numbers, you can use the spread operator like this: Math.max(...scoresArray))

4. Constants The Math object also stores common mathematical constants.

console.log(Math.PI); // 3.14159...

💡 Himanshu's Tip:

The most common mistake I see beginners make when generating a random number is forgetting to wrap Math.random() inside a Math.floor(). Without Math.floor(), you will end up with numbers like 7.34829374 instead of a clean 7!

Interview Questions

  1. Why do we not need to instantiate the Math object using the new keyword?
  2. Write a one-line formula using the Math object to generate a random integer between 1 and 100.
  3. What is the difference between Math.floor() and Math.trunc()?

Design & Developed by Himanshu Dubey © 2026