Rounding
round, floor, ceil and trunc handle decimals differently.
The Math object contains common numeric helpers: rounding, min, max, powers, square roots, absolute values and random numbers.
Math.round(4.6); Math.max(12, 4, 28); Math.sqrt(81); Math.random();
Strings, Numbers & Math
Math is a built-in object with static methods and constants. You do not create it with new Math(). You call methods directly: Math.round, Math.max, Math.min and so on.
Rounding methods have different meanings. Math.round rounds to the nearest integer, Math.floor rounds down, Math.ceil rounds up and Math.trunc removes the fractional part.
Math.random returns a decimal from 0 up to, but not including, 1. It is useful for simple UI randomness, but not for security-sensitive values.
round, floor, ceil and trunc handle decimals differently.
min and max pick the smallest or largest value.
sqrt, pow, hypot and constants help with numeric formulas.
Math.random creates non-secure pseudo-random decimals.
Examples
const value = 4.2; console.log(Math.floor(value)); // 4 console.log(Math.ceil(value)); // 5 console.log(Math.round(value)); // 4
const pages = 4.2; const visiblePages = Math.round(pages); // If partial pages require another page, Math.ceil is the better choice.
Code patterns
These small examples are designed for scanning. Use them when you need the syntax quickly, then read the surrounding notes when you want the deeper reason behind the pattern.
Use Math.round for nearest integer output.
const score = 82.6; console.log(Math.round(score)); // 83
Combine Math.max and Math.min to keep a value in range.
const value = 120; const clamped = Math.min(Math.max(value, 0), 100); console.log(clamped); // 100
Spread an array into Math.max for small arrays.
const scores = [72, 88, 91]; const highest = Math.max(...scores); console.log(highest);
Use floor to turn a random decimal into an array index.
const items = ["alpha", "beta", "gamma"]; const index = Math.floor(Math.random() * items.length); console.log(items[index]);
Rules that matter
Strings and numbers are simple until they cross a boundary: form input, API data, generated output, rounding, formatting or search. Normalize, validate and format deliberately.
floor, ceil, round and trunc solve different problems.
This is a common safe-range pattern.
It is not cryptographically secure.
Math.max(...[]) returns -Infinity.
Math.PI is clearer than typing a rounded pi value manually.
Complex formulas deserve intermediate variables.
Production thinking
Math helpers make numeric code shorter, but the wrong helper can create subtly wrong behavior.
Numeric UI states such as progress and ratings should be rounded and bounded in ways users can understand.
Production math should document assumptions, handle empty values and avoid security mistakes with random numbers.
Generated ratings, percentages and counts should be rounded consistently when shown as visible or structured content.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.