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
Mathis a static object — nonew Math()needed.- All angle functions use radians, not degrees.
Math.random()returns a value0 ≤ x < 1.- Use
Math.floor(Math.random() * n)for random integers.
Common Math Methods
| Method | Description | Example → Output |
|---|---|---|
| Math.floor() | Round down | Math.floor(4.9) → 4 |
| Math.ceil() | Round up | Math.ceil(4.1) → 5 |
| Math.round() | Round nearest | Math.round(4.5) → 5 |
| Math.trunc() | Remove decimals | Math.trunc(4.7) → 4 |
| Math.abs() | Absolute value | Math.abs(-9) → 9 |
| Math.pow() | Power | Math.pow(2,5) → 32 |
| Math.sqrt() | Square root | Math.sqrt(81) → 9 |
| Math.cbrt() | Cube root | Math.cbrt(27) → 3 |
| Math.min() | Smallest value | Math.min(3,7,1) → 1 |
| Math.max() | Largest value | Math.max(3,7,1) → 7 |
| Math.random() | Random 0–1 | 0.7321… |
| Math.PI | π constant | 3.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
<script> let r = 7; let area = Math.PI * r * r; document.write( "Area = " + area.toFixed(2) ); </script>
Example 1 : Rounding Methods
<script> document.write( Math.floor(4.8) + "<br>" ); document.write( Math.ceil(4.2) + "<br>" ); document.write( Math.round(4.5) ); </script>
5
5
Example 2 : Power and Square Root
<script> document.write( Math.pow(3,4) + "<br>" ); document.write( Math.sqrt(144) ); </script>
12
Example 3 : Random Number
<script> let n = Math.floor( Math.random()*100 ) + 1; document.write( "Random = " + n ); </script>
Example 4 : Min and Max
<script> let arr = [22,5,38,14,7]; document.write( "Min = " + Math.min(...arr) + "<br>" ); document.write( "Max = " + Math.max(...arr) ); </script>
Max = 38
Example 5 : cbrt() Function
<script> document.write( Math.cbrt(125) ); </script>
Example 6 : Euler's Number
<script> document.write( Math.E ); </script>
Example 7 : trunc()
<script> document.write( Math.trunc(8.76) ); </script>
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