Numbers & Math
Prices, scores, percentages, coordinates, timers — numbers run through every app. JavaScript has just one number type for integers and decimals alike, which is convenient until you hit the famous 0.1 + 0.2 surprise.
Learn Numbers & Math in our free JavaScript course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
This lesson covers arithmetic, rounding, the Math object, conversions, and how to handle precision like a professional.
What You'll Learn in This Lesson
📐 Real-World Analogy: Think of a kitchen scale that only shows two decimal places:
- • The scale stores far more digits internally than it shows you
- • Tiny rounding at the last digit is invisible until you add many readings
- • For a recipe (display) you round; for selling by exact weight (money) you use whole grams
- • That's exactly why we round for display and use integer cents for currency
🧠 Arithmetic & the Modulo Operator
The basic operators are + - * / , plus % (remainder) and ** (exponent). The remainder operator is the secret behind "is this even?" and "wrap around a clock":
⚠️ The Floating-Point Gotcha
Every JavaScript number is a 64-bit float, so some decimals can't be stored exactly. This is not a bug in your code — it happens in nearly every programming language:
🔘 Rounding the Right Way
Different jobs need different rounding. Pick deliberately — the wrong one silently corrupts totals:
Note the negative trap: Math.floor(-4.7) is -5 because floor always moves toward negative infinity, while Math.trunc just deletes the fractional part.
🧮 The Math Object & Random Numbers
Math is a built-in toolbox of static helpers. You don't create it — you call methods on it directly. Random integers are the most-asked-for recipe:
🔁 Converting Strings to Numbers
User input always arrives as text. Convert it carefully — the method you choose changes the result:
🚫 NaN, Infinity & Safe Checks
Two special numeric values appear when math goes sideways. Knowing how to detect them prevents hard-to-trace bugs:
🧩 Reorder Challenge
These lines should compute a price with 20% tax and print it as currency. They are scrambled — reorder them so the log is $24.00 .
Why: the base price (C) must exist before tax is computed from it (A). The total (D) adds the two together, and only then can it be formatted (B). Working in integer cents avoids the floating-point error you'd hit doing 20 * 1.2 directly, and Math.round keeps the tax a whole number of cents.
🧠 Quick Recall
55 — Number("5") is the number 5, but 5 + "5" triggers string concatenation (the number is coerced to text), producing "55" . If you wanted 10, both operands must be numbers.
-3 — Math.floor always rounds toward negative infinity, so -2.1 becomes -3 , not -2 . Use Math.trunc(-2.1) if you want -2 .
false — 0 / 0 is NaN , and NaN is the only value in JavaScript that is not equal to itself. That's exactly why you must use Number.isNaN(x) to detect it.
⚠️ Common Mistakes to Avoid
- 1️⃣ Comparing decimals with ===. 0.1 + 0.2 === 0.3 is false ; round or use an epsilon.
- 2️⃣ Comparing to NaN. x === NaN never works — use Number.isNaN(x) .
- 3️⃣ Forgetting Number("") is 0. Empty input quietly becomes zero, hiding validation bugs.
- 4️⃣ Using floats for money. Store cents as integers and divide only when displaying.
Frequently Asked Questions
Lesson Complete — Numbers & Math!
- ✅ One number type covers integers and decimals; % gives the remainder
- ✅ Floating-point isn't exact — round for display, use cents for money
- ✅ Choose rounding deliberately: round , floor , ceil , trunc
- ✅ Detect bad values with Number.isNaN and Number.isFinite
Up next: Loops & Iteration — repeat work efficiently with for, while, and for…of! 🔁
Practice quiz
What does console.log(17 % 4) output?
- 4
- 1
- 4.25
- 0
Answer: 1. The % operator returns the remainder: 17 divided by 4 is 4 with 1 left over.
What does console.log(2 ** 10) output?
- 20
- 100
- 1024
- 12
Answer: 1024. ** is the exponent operator, so 2 ** 10 is 2 to the 10th power, which is 1024.
What does console.log(0.1 + 0.2 === 0.3) output?
- true
- false
- NaN
- 0.3
Answer: false. 0.1 + 0.2 equals 0.30000000000000004 due to floating-point rounding, so it is not strictly equal to 0.3.
What is the value of Math.floor(-4.7)?
- -4
- -5
- 4
- 5
Answer: -5. Math.floor always rounds toward negative infinity, so -4.7 becomes -5.
Which method just chops off the decimal part without rounding, giving -4 from -4.7?
- Math.floor
- Math.round
- Math.ceil
- Math.trunc
Answer: Math.trunc. Math.trunc(-4.7) deletes the fractional part and returns -4, unlike Math.floor which gives -5.
What does console.log(Number('12px')) output?
- 12
- NaN
- 0
- '12px'
Answer: NaN. Number requires the whole string to be numeric; '12px' has trailing letters, so it returns NaN.
What does console.log(parseInt('12px')) output?
- NaN
- 0
- 12
- '12'
Answer: 12. parseInt reads digits from the front and stops at the first non-digit, so it returns 12.
What does console.log(Number('')) output?
- NaN
- 0
- undefined
- ''
Answer: 0. An empty string converts to 0 in JavaScript, a classic trap that can hide validation bugs.
Why does x === NaN return false even when x is NaN?
- NaN is never equal to itself
- === does not work on numbers
- NaN equals 0
- It actually returns true
Answer: NaN is never equal to itself. NaN is the only value not equal to itself, so you must use Number.isNaN(x) to detect it reliably.
What does console.log(Number('5') + '5') output?
- 10
- '55'
- NaN
- 55 as a number
Answer: '55'. Number('5') is the number 5, but adding a string coerces it to text, producing the string '55'.
Continue this course
- Previous: Strings & String Methods
- Next: Loops & Iteration