JavaScript Math Functions — PBA Institute Tutorial
Chapter 07 · JavaScript Programming Series
12 min read Beginner

JavaScript Math Functions

The JavaScript Math object provides built-in methods and constants for mathematical work — rounding, powers, roots, random numbers, and more. It needs no instance, just call Math.method() directly.

What is the Math Object?

Math is a built-in JavaScript object that holds mathematical properties (like Math.PI) and methods (like Math.sqrt). You do not create instances of Math — just call methods directly.

Common Math Categories

🪙

Rounding

floor, ceil, round, trunc — control how decimals become integers.

Powers / Roots

pow, sqrt, cbrt for exponents and roots.

📏

Min / Max

Math.min and Math.max compare any number of values.

🎲

Random

Math.random() returns a number from 0 to 1.

📐

Trigonometry

sin, cos, tan, atan — angles in radians.

🔣

Constants

PI, E, LN2, SQRT2 — useful math constants.

Basic Features

  • Math is a static object — no new Math() needed.
  • All angle functions use radians, not degrees.
  • Math.random() returns a value 0 ≤ x < 1.
  • Use Math.floor(Math.random() * n) for random integers.

Common Math Methods

MethodDescriptionExample → Output
Math.floor()Round downMath.floor(4.9) → 4
Math.ceil()Round upMath.ceil(4.1) → 5
Math.round()Round nearestMath.round(4.5) → 5
Math.trunc()Remove decimalsMath.trunc(4.7) → 4
Math.abs()Absolute valueMath.abs(-9) → 9
Math.pow()PowerMath.pow(2,5) → 32
Math.sqrt()Square rootMath.sqrt(81) → 9
Math.cbrt()Cube rootMath.cbrt(27) → 3
Math.min()Smallest valueMath.min(3,7,1) → 1
Math.max()Largest valueMath.max(3,7,1) → 7
Math.random()Random 0–10.7321…
Math.PIπ constant3.14159…

Math Functions in JavaScript

Math functions in JavaScript are used to perform mathematical operations like addition, subtraction, rounding, trigonometry, random number generation, square roots, powers, and more.

Why Math Functions are Needed ?

  • Perform basic arithmetic operations.
  • Used in scientific calculations.
  • Essential in game development.
  • Used for finance and statistics.
  • Helpful in graphics and animations.

Your First Math Program

Area of Circle using Math.PI
<script>

let r = 7;

let area =

Math.PI * r * r;

document.write(

"Area = " +

area.toFixed(2)

);

</script>
Output Area = 153.94

Example 1 : Rounding Methods

floor(), ceil(), round()
<script>

document.write(

Math.floor(4.8)

+ "<br>"

);

document.write(

Math.ceil(4.2)

+ "<br>"

);

document.write(

Math.round(4.5)

);

</script>
Output 4
5
5

Example 2 : Power and Square Root

pow() and sqrt()
<script>

document.write(

Math.pow(3,4)

+ "<br>"

);

document.write(

Math.sqrt(144)

);

</script>
Output 81
12

Example 3 : Random Number

Random Number 1–100
<script>

let n =

Math.floor(

Math.random()*100

) + 1;

document.write(

"Random = " + n

);

</script>
Output Random = 47

Example 4 : Min and Max

Find Minimum and Maximum
<script>

let arr =

[22,5,38,14,7];

document.write(

"Min = " +

Math.min(...arr)

+ "<br>"

);

document.write(

"Max = " +

Math.max(...arr)

);

</script>
Output Min = 5
Max = 38

Example 5 : cbrt() Function

Cube Root Example
<script>

document.write(

Math.cbrt(125)

);

</script>
Output 5

Example 6 : Euler's Number

Math.E Example
<script>

document.write(

Math.E

);

</script>
Output 2.718281828459045

Example 7 : trunc()

Remove Decimal Part
<script>

document.write(

Math.trunc(8.76)

);

</script>
Output 8

Important Notes

  • Math.random() never returns 1.
  • Use toFixed() for decimal formatting.
  • Trigonometric functions use radians.
  • JavaScript uses 64-bit floating values.

Real Life Use Cases

🎮

Games

Random enemy positions and dice rolls.

📐

Graphics

Animation and rotation calculations.

💰

Finance

EMI and interest calculations.

Frequently Asked Questions

Question Answer
Is Math a class? No, it is a built-in object.
Does Math.random() return 1? No, it always returns less than 1.
What is Math.PI ? Constant value of π.

Conclusion

JavaScript Math functions help developers perform calculations, generate random numbers, build games, process scientific data, and create dynamic applications.

JavaScript All Chapters

Continue Learning

Previous

Go to Function Chapter

Next

Go to Switch Case Chapter