Useful Mathematical Functions in JavaScript (English)

This article explains commonly used mathematical functions in JavaScript and provides sample code for each function.

Method

  1. Math.floor()

  2. Math.ceil()

  3. Math.round()

  4. Math.random()

  5. Math.max()

  6. Math.min()

  7. Math.pow(base, exponent)

  8. Math.abs()

  9. Math.sqrt()

  10. Math.cbrt()

  11. Math.PI

  12. Math.sin(), Math.cos(), Math.tan()

  13. Math.log()

  14. Math.log10()


Math.floor()

This function returns the integer after truncating the decimal part.

Math.floor()

let num = 5.67;
let result = Math.floor(num); // 5

Math.ceil()

This function returns the integer after rounding up the decimal part.

Math.ceil()

let num = 5.23;
let result = Math.ceil(num); // 6

Math.round()

This function returns the value after rounding it to the nearest integer.

Math.round()

let num = 5.5;
let result = Math.round(num); // 6

Math.random()

This function returns a random number between 0 and 1.

Math.random()

let randomNum = Math.random(); // 0.123

Math.max()

This function returns the maximum value among the given numbers.

Math.max()

let result = Math.max(10, 5, 8); // 10

Math.min()

This function returns the minimum value among the given numbers.

Math.min()

let result = Math.min(10, 5, 8); // 5

Math.pow(base, exponent)

This function returns the value of a base raised to the power of an exponent.

Math.pow(base, exponent)

let result = Math.pow(2, 3); // 8 (2^3)

Math.abs()

This function returns the absolute value of the given number.

Math.abs()

let num = -7;
let result = Math.abs(num); // 7

Math.sqrt()

This function returns the square root of the given number.

Math.sqrt()

let result = Math.sqrt(16); // 4 (√16)

Math.cbrt()

This function returns the cube root of the given number.

Math.cbrt()

let result = Math.cbrt(27); // 3 (³√27)

Math.PI

This function returns an approximation of the mathematical constant pi (π).

Math.PI

let piValue = Math.PI; // 3.141592653589793

Math.sin(), Math.cos(), Math.tan()

This function returns the sine, cosine, and tangent values of the given angle (in radians).

Math.sin(), Math.cos(), Math.tan()

let angle = Math.PI / 2; // 90도를 라디안으로 표현한 값
let sinValue = Math.sin(angle); // 결과: 1 (sin(90°) = 1)
let cosValue = Math.cos(angle); // 결과: 0 (cos(90°) = 0)
let tanValue = Math.tan(angle); // 결과: Infinity (tan(90°) = ∞)


Math.log()

This function returns the natural logarithm of the given number.

Math.log()

let result = Math.log(10); // 2.302585092994046 (ln(10))

Math.log10()

This function returns the base 10 logarithm (common logarithm) of the given number.

Math.log10()

let result = Math.log10(100); // 2 (log10(100) = 2)