J

JavaScript Handbook

Clean • Professional

JavaScript Math — Top Interview Questions & Answers

3 minute

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?

MethodDescriptionExampleResult
Math.round(x)Rounds to nearest integerMath.round(4.6)5
Math.floor(x)Rounds downMath.floor(4.9)4
Math.ceil(x)Rounds upMath.ceil(4.1)5
Math.trunc(x)Removes decimal partMath.trunc(4.8)4

Ques: What is the difference between Math.floor() and Math.trunc()?

FunctionDescriptionExample
Math.floor()Always rounds downMath.floor(-4.2)-5
Math.trunc()Removes decimal partMath.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?

MethodDescriptionExample
Math.sin(x)Returns sine (in radians)Math.sin(Math.PI/2)1
Math.cos(x)Returns cosineMath.cos(0)1
Math.tan(x)Returns tangentMath.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?

MethodDescriptionExample
Math.log(x)Natural log (base e)Math.log(Math.E)1
Math.log10(x)Base-10 logMath.log10(100)2
Math.exp(x)e raised to power xMath.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?

ConstantValueDescription
Math.E2.718Euler’s number
Math.PI3.14159Pi
Math.SQRT21.414√2
Math.SQRT1_20.707√(1/2)
Math.LN20.693ln(2)
Math.LN102.303ln(10)
Math.LOG2E1.442log₂(e)
Math.LOG10E0.434log₁₀(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.

Article 0 of 0