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
Output
The Math object does not require creating an instance using new. It is accessed directly using Math.
Example
console.log(Math.PI);Output
3.141592653589793Q2. 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.
Output
Math.floor()
Rounds a number downward.
Output
Math.ceil
Round a number upward.
Output
Math.trunc()
Removes the decimal part.
Output
Math.round()
Rounds a number to the nearest integer.
console.log(Math.round(4.6));Output
5Math.floor()
Rounds a number downward.
console.log(Math.floor(4.9));Output
4Math.ceil
Round a number upward.
console.log(Math.ceil(4.1));Output
5Math.trunc()
Removes the decimal part.
console.log(Math.trunc(4.9));Output
4Q3. How can you generate random numbers in JavaScript?
JavaScript provides the Math.random() method to generate random numbers between 0 and 1.
Example
Generates a random decimal number between 0 and 1.
Generate Random Number Between 1 and 10
Explanation:
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
Real World Use Case
This function calculates the area of a circle.
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.
