Floating-point
Decimals can produce tiny precision differences.
Floating-point precision and random values are two places where JavaScript numbers need extra care.
0.1 + 0.2; // 0.30000000000000004 Math.floor(Math.random() * 10); crypto.getRandomValues(new Uint32Array(1));
Strings, Numbers & Math
JavaScript numbers use binary floating-point. Many decimal fractions cannot be represented exactly, so tiny rounding errors can appear.
For display, rounding may be enough. For exact decimal domains, store integer units where possible or use a decimal library when accuracy requirements are strict.
Math.random is fine for simple visual randomness, sampling and small demos. It is not secure. For secure random values in the browser, use crypto.getRandomValues or crypto.randomUUID where appropriate.
Decimals can produce tiny precision differences.
Store exact quantities as integers when possible.
Useful for non-secure random UI behavior.
Use Web Crypto for security-sensitive randomness.
Examples
const unitA = 10; const unitB = 20; const totalUnits = unitA + unitB; console.log(totalUnits); // 30
const total = 0.1 + 0.2; console.log(total === 0.3); // false
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.
Good for visible percentages and approximate values.
const value = 0.1 + 0.2; console.log(value.toFixed(2)); // "0.30"
Avoid decimal drift by calculating in smaller whole units.
const centsA = 10; const centsB = 20; const totalCents = centsA + centsB; console.log(totalCents); // 30
The common non-secure pattern for simple UI use.
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randomInt(1, 6));
Use browser crypto for values that should not be predictable.
const id = crypto.randomUUID(); console.log(id);
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.
Tiny floating-point differences are normal for many decimals.
Rounding hides display noise but should not define core data rules by accident.
Whole-unit calculations are easier to keep exact.
It is not for tokens, passwords or security decisions.
Web Crypto provides stronger browser randomness.
Random range functions should clearly define inclusive and exclusive bounds.
Production thinking
Precision and randomness bugs can be hard to see. The code runs, but the number is slightly wrong or the random value is not safe enough.
Random UI behavior should not make interfaces unpredictable for keyboard users or assistive technology users.
Production code should separate approximate display math from exact business rules and use secure randomness for security-sensitive values.
Random rendering should not change important content, headings or links in ways that make pages inconsistent for crawlers and users.
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.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build a small example that combines three lessons from this chapter.
Can you explain the important tradeoff without reading from the page?