JavaScript Math & Math Reference — Interview Questions & Answers
Ques: What is the Math object in JavaScript?
Ans: The Math object in JavaScript is a built-in static object that provides mathematical constants and methods for performing numeric calculations like rounding, trigonometry, logarithms, random numbers, etc.
Example:
console.log(Math.PI); // 3.141592653589793
console.log(Math.sqrt(25)); // 5
Ques: What are the features of the Math object?
- Contains static methods (no need to create an instance)
- Provides mathematical constants (
PI,E, etc.) - Used for rounding, powers, roots, min/max, etc.
- Supports random number generation
Ques: How do you get the value of π (pi)?
Math.PI; // 3.141592653589793
Ques: How do you round numbers in JavaScript?
| Method | Description | Example | Result |
|---|---|---|---|
Math.round(x) | Rounds to nearest integer | Math.round(4.6) | 5 |
Math.floor(x) | Rounds down | Math.floor(4.9) | 4 |
Math.ceil(x) | Rounds up | Math.ceil(4.1) | 5 |
Math.trunc(x) | Removes decimal part | Math.trunc(4.8) | 4 |
Ques: What is the difference between Math.floor() and Math.trunc()?
| Function | Description | Example |
|---|---|---|
Math.floor() | Always rounds down | Math.floor(-4.2) → -5 |
Math.trunc() | Removes decimal part | Math.trunc(-4.2) → -4 |
Ques: How do you get the absolute value of a number?
Ans: Returns the non-negative value of a number.
Math.abs(-7); // 7
Ques: How to get the maximum or minimum of several numbers?
Math.max(4, 9, 2); // 9
Math.min(4, 9, 2); // 2
You can also use the spread operator:
let nums = [10, 25, 5];
Math.max(...nums); // 25
Ques: How to generate a random number in JavaScript?
Math.random(); // Returns a number between 0 (inclusive) and 1 (exclusive)
Example: Generate a random number between 1 and 10:
Math.floor(Math.random() * 10) + 1;
Ques: How to find the square root of a number?
Math.sqrt(49); // 7
Ques: How to calculate powers and exponents?
Math.pow(2, 3); // 8
// or modern syntax:
2 ** 3; // 8
Ques: How do you find the cube root or nth root of a number?
Math.cbrt(27); // 3
Math.pow(16, 1/4); // 4th root = 2
Ques: What are trigonometric methods in Math?
| Method | Description | Example |
|---|---|---|
Math.sin(x) | Returns sine (in radians) | Math.sin(Math.PI/2) → 1 |
Math.cos(x) | Returns cosine | Math.cos(0) → 1 |
Math.tan(x) | Returns tangent | Math.tan(Math.PI/4) → 1 |
Inverse trigonometric functions:
Math.asin(x)Math.acos(x)Math.atan(x)
Ques: What are logarithmic and exponential functions in Math?
| Method | Description | Example |
|---|---|---|
Math.log(x) | Natural log (base e) | Math.log(Math.E) → 1 |
Math.log10(x) | Base-10 log | Math.log10(100) → 2 |
Math.exp(x) | e raised to power x | Math.exp(1) → 2.718... |
Ques: How to find the sign of a number?
Math.sign(7); // 1
Math.sign(-5); // -1
Math.sign(0); // 0
Ques: How to work with degrees instead of radians in trigonometric functions?
Ans: JavaScript trigonometric functions use radians, not degrees.
Convert degrees to radians:
function degToRad(deg) {
return deg * (Math.PI / 180);
}
console.log(Math.sin(degToRad(90))); // 1
Ques: How to generate a random integer within a specific range?
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomInt(1, 100); // returns random integer between 1 and 100
Ques: What constants are available in the Math object?
| Constant | Value | Description |
|---|---|---|
Math.E | 2.718 | Euler’s number |
Math.PI | 3.14159 | Pi |
Math.SQRT2 | 1.414 | √2 |
Math.SQRT1_2 | 0.707 | √(1/2) |
Math.LN2 | 0.693 | ln(2) |
Math.LN10 | 2.303 | ln(10) |
Math.LOG2E | 1.442 | log₂(e) |
Math.LOG10E | 0.434 | log₁₀(e) |
Ques: What does Math.hypot() do?
Ans: Returns the square root of the sum of squares of arguments (useful for distance calculations).
Math.hypot(3, 4); // 5
Ques: What is the use of Math.clz32()?
Ans: Returns the number of leading zero bits in the 32-bit binary representation of a number.
Math.clz32(1); // 31
Math.clz32(1000); // 22
Ques: Is Math mutable or static?
Ans: Math is a static object, meaning:
- You can’t instantiate it using
new Math(). - You can’t modify its properties or constants.
