Loading

Quipoin Menu

Learn • Practice • Grow

java-script / Math in JavaScript
interview

Q1. What is the Math object in JavaScript?
The Math object in JavaScript is a built-in object that provides mathematical constants and functions. It allows developers to perform mathematical operations like rounding numbers, finding square roots, generating random numbers, and performing trigonometric calculations.
The Math object does not require creating an instance using new. It is accessed directly using Math.

Example
console.log(Math.PI);

Output
3.141592653589793


Q2. What are some commonly used Math methods in JavaScript?
JavaScript provides several useful methods inside the Math object.

Math.round()
Rounds a number to the nearest integer.
console.log(Math.round(4.6));

Output
5


Math.floor()
Rounds a number downward.
console.log(Math.floor(4.9));

Output
4


Math.ceil
Round a number upward.
console.log(Math.ceil(4.1));

Output
5


Math.trunc()
Removes the decimal part.
console.log(Math.trunc(4.9));


Output
4


Q3. How can you generate random numbers in JavaScript?
JavaScript provides the Math.random() method to generate random numbers between 0 and 1.

Example
console.log(Math.random());

Generates a random decimal number between 0 and 1.


Generate Random Number Between 1 and 10
let randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);

Explanation:
  • Math.random() → Generates decimal value
  • * 10 → Expands range
  • Math.floor() → Removes decimal part
  • + 1 → Ensures number starts from 1


Q4. What are mathematical constants available in JavaScript?
The Math object provides predefined constants used in mathematical calculations.

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

Real World Use Case
function calculateCircleArea(radius) {
    return Math.PI * Math.pow(radius, 2);
}

console.log(calculateCircleArea(5));

This function calculates the area of a circle.