FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Strings, Numbers & Math

Intermediate

Math

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 toolbox, not a constructor.

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.

Rounding

round, floor, ceil and trunc handle decimals differently.

Bounds

min and max pick the smallest or largest value.

Geometry helpers

sqrt, pow, hypot and constants help with numeric formulas.

Random

Math.random creates non-secure pseudo-random decimals.

Examples

Start with the smallest working pattern.

Choose the rounding method on purpose

const value = 4.2;

console.log(Math.floor(value)); // 4
console.log(Math.ceil(value));  // 5
console.log(Math.round(value)); // 4

Rounding without knowing the rule

const pages = 4.2;
const visiblePages = Math.round(pages);

// If partial pages require another page, Math.ceil is the better choice.

Code patterns

Reusable examples for quick reference.

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.

Round a score

Use Math.round for nearest integer output.

const score = 82.6;

console.log(Math.round(score)); // 83

Clamp a value

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

Find a maximum

Spread an array into Math.max for small arrays.

const scores = [72, 88, 91];
const highest = Math.max(...scores);

console.log(highest);

Generate a simple random index

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

Keep text and numbers predictable.

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.

Pick rounding by meaning

floor, ceil, round and trunc solve different problems.

Clamp with min and max

This is a common safe-range pattern.

Do not use Math.random for secrets

It is not cryptographically secure.

Watch empty arrays with Math.max

Math.max(...[]) returns -Infinity.

Use constants when needed

Math.PI is clearer than typing a rounded pi value manually.

Keep formulas named

Complex formulas deserve intermediate variables.

Production thinking

Small value mistakes become visible product mistakes.

Why does this matter?

Math helpers make numeric code shorter, but the wrong helper can create subtly wrong behavior.

Accessibility

Numeric UI states such as progress and ratings should be rounded and bounded in ways users can understand.

Production note

Production math should document assumptions, handle empty values and avoid security mistakes with random numbers.

SEO note

Generated ratings, percentages and counts should be rounded consistently when shown as visible or structured content.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

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

Try this now.

  • Change Math.round to Math.floor and Math.ceil.
  • Change the clamp range from 0-100 to 10-90.
  • Generate a random item from an array.

Practice assignment

Do this before moving to the next topic.

  1. Use Math.max to find the largest of three values.
  2. Use Math.ceil when a partial unit still needs a full unit.
  3. Use Math.floor with Math.random to create an array index.

Try it yourself

Clamp a progress value

Live preview

Self-check

Before you continue, prove that you understand Math.

Intermediate

Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.

  1. Can you explain why Math is not constructed?
  2. Can you explain the difference between floor, ceil and round?
  3. Can you explain the clamp pattern?
  4. Can you explain why Math.random is not secure?
  5. Can you explain what happens with Math.max(...[])?