JS Sketchbooksee JavaScript think ✏️
← back to phase 1

1.5 — Numbers

Numbers are the workhorse type: scores, prices, timeouts, how many tests passed. The good news — JavaScript keeps it simple: one number type for everything, whole or decimal, positive or negative.

The spicy news: at the end of this lesson we’ll ask the machine for 0.1 + 0.2 and it will answer slightly wrong — famously, honestly, and for a reason you’ll be able to explain at parties (well… programmer parties).

watch it happen
console.log(4 + 3);
console.log(7 / 2);
console.log(10 % 3);
console.log(0.1 + 0.2);

Picture every number living on a line. Math operations are just movement on it: + hops right, − hops left, * and / stretch and squeeze. The operators you know — plus one operator nobody learned in school, coming up.

the number line — where all math lives012345678910
under the hood

The deeper story, with the real names for things — this part is what turns “I saw it” into “I can explain it.”

The storage format has a name worth knowing: floating point. The standard is called IEEE 754, and every mainstream language uses it. That is why Python and Java give the same 0.30000000000000004.

Each number gets 64 bits — 64 tiny on/off switches. That is plenty for whole numbers, up to about 9 quadrillion (Number.MAX_SAFE_INTEGER). But some decimals must be stored as their nearest storable neighbor. Whole numbers are always exact; only certain fractions get trimmed.

One special citizen of the number type, born from impossible math: 1 / 0 gives Infinity — no crash, just a special value meaning “beyond all numbers.”

Another: 0 / 0 gives NaN — “Not a Number”. Ironically, NaN is itself a value of type number. It means “this calculation lost all meaning.” NaN in a test report means some math earlier went wrong. It spreads through calculations like ink in water — hunt for where it was born.

Practical toolkit: Math.round(x), Math.floor(x) (always down), Math.ceil(x) (always up), and x.toFixed(2) for showing “3.50” to humans.

For comparing decimals in tests, remember one pattern — “the difference is smaller than a speck”: Math.abs(a - b) < 0.000001. Test frameworks wrap exactly this idea in an assertion called toBeCloseTo. You will use it in Phase 10.

your turn

⌨️ the machine does your math

Compute an age from a birth year. Programmers don’t do arithmetic in their heads — they write the formula, so the answer stays right when the inputs change.

requirements:

  • birthYear holds the number 2000 and never changes.
  • A variable named age gets the age in the year 2026 — calculated by the machine from those two numbers. The number 26 may not appear in your code.
  • Print age.

when you press RUN, the console must show exactly:

26

✏️ Quick check 1

Type what 9 % 4 gives:

✏️ Quick check 2

WHY does 0.1 + 0.2 come out as 0.30000000000000004?

✏️ Quick check 3

Type exactly what 1 / 0 gives in JavaScript — capitalization counts:

teach it back

🗣️ Now teach it back

Explain to a friend why the computer says 0.1 + 0.2 is 0.30000000000000004 — using the 1/3 analogy — and what programmers do about it.

Write it as if your friend is sitting next to you. Saved to your journal — future-you will use these notes to teach others.

a few sentences, minimum — you’ve got this
to remember
One number type for everything — whole, decimal, negative. 7 / 2 = 3.5, no ceremony.
% gives the remainder: 10 % 3 = 1. Party trick: n % 2 === 0 means even.
0.1 + 0.2 = 0.30000000000000004 because binary can’t store 0.1 exactly (like 1/3 in decimal). Compare decimals with tolerance, count money in integers.
next: 1.6 Strings →