Variables & Data Lesson 10 of 30
JavaScript Numbers and the Math Object
Convert text to numbers, round money with toFixed, use Math.round, floor, ceil, max, min and Math.random to make dice and random pickers.
Grades, prices, scores and timers are all numbers. JavaScript gives you tools to convert, round and compute them, plus a built-in Math object full of useful functions.
Turning text into numbers
Values from input boxes are always strings. Convert them before doing math:
| Code | Result | Notes |
|---|---|---|
Number("85") | 85 | Whole string must be a number |
Number("85.5") | 85.5 | Decimals OK |
Number("") | 0 | Careful: empty becomes 0 |
Number("abc") | NaN | "Not a Number" |
parseInt("12 pcs") | 12 | Reads digits from the start |
parseFloat("3.5kg") | 3.5 | Same, with decimals |
NaN: Not a Number
NaN appears when math fails, like Number("abc") or "hello" * 2. Check for it with Number.isNaN(value):
<input id="age" placeholder="Your age">
<button onclick="check()">OK</button>
<p id="msg"></p>
<script>
function check() {
const age = Number(document.getElementById("age").value);
const msg = Number.isNaN(age) ? "Please type a number." : `In 5 years you'll be ${age + 5}.`;
document.getElementById("msg").textContent = msg;
}
</script>
Rounding money: toFixed()
toFixed(n) rounds to n decimal places and returns a string, ready to display:
(0.1 + 0.2).toFixed(2) // "0.30"
(1234.5).toFixed(2) // "1234.50"
(89.456).toFixed(1) // "89.5"
For pesos with commas, use toLocaleString:
(1234567.5).toLocaleString("en-PH", { style: "currency", currency: "PHP" })
// "₱1,234,567.50"
The Math object
| Function | What it does | Example → result |
|---|---|---|
Math.round(x) | Nearest whole number | Math.round(89.5) → 90 |
Math.floor(x) | Round down | Math.floor(89.9) → 89 |
Math.ceil(x) | Round up | Math.ceil(41 / 5) → 9 |
Math.max(...) | Largest | Math.max(88, 95, 79) → 95 |
Math.min(...) | Smallest | Math.min(88, 95, 79) → 79 |
Math.abs(x) | Remove the minus sign | Math.abs(-7) → 7 |
Math.sqrt(x) | Square root | Math.sqrt(144) → 12 |
Math.PI | π | 3.14159… |
Math.ceil answers questions like "41 students, 5 per tricycle: how many tricycles?" The answer is 9, not 8.2.
<p id="out"></p>
<script>
const radius = 3; // meters
const area = Math.PI * radius ** 2;
document.getElementById("out").textContent = `Area: ${area.toFixed(2)} square meters`;
</script>
Random numbers
Math.random() gives a random decimal from 0 up to (but not including) 1, like 0.7351. To get a random whole number from 1 to 6, like a die:
Math.floor(Math.random() * 6) + 1
How it works: Math.random() * 6 gives 0 to 5.999…, Math.floor turns that into 0–5, and + 1 shifts it to 1–6.
<button onclick="roll()">🎲 Roll</button>
<p id="out" style="font-size:40px;margin:8px 0"></p>
<script>
function roll() {
const a = Math.floor(Math.random() * 6) + 1;
const b = Math.floor(Math.random() * 6) + 1;
document.getElementById("out").textContent = `${a} + ${b} = ${a + b}`;
}
</script>
A reusable random-between function
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
randomBetween(1, 40); // pick a random class number for recitation
Increasing precision is not free
Very large numbers lose accuracy: JavaScript numbers are exact only up to 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER). That's plenty for grades and prices. For bigger whole numbers there's a separate type called BigInt, written with an n at the end, like 123n.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04