TypeScript from Zero: Numbers and Math
November 6, 2026
Episode 3 of TypeScript from Zero: numbers and math. TypeScript has one type for numbers, called number; whole numbers and decimals are both numbers. We use the operators and the Math helpers, meet the famous 0.1 + 0.2 surprise and NaN, and fix a typo with tsc. All code below is the final code from the video.
- Previous: Variables with let and const. All episodes: playlist.
cd hello-ts
touch numbers.ts
code .1. Plus and minus
const apples = 7;
const people = 3;
console.log(apples + people);
console.log(apples - people);10
42. Times and divide
* multiplies and / divides. Division always gives the exact answer, not a rounded one:
console.log(apples * people);
console.log(apples / people);21
2.33333333333333353. Remainder and power
% gives the remainder (handy to check if a number is even). ** is "to the power of":
console.log(apples % people);
console.log(2 ** 10);1
10244. The Math helpers
console.log(Math.round(apples / people));
console.log(Math.max(4, 9, 2));2
95. 0.1 + 0.2
console.log(0.1 + 0.2);0.30000000000000004Computers store decimals in binary, and some decimals do not fit exactly. This is true in every language. For money, count cents as whole numbers.
6. NaN
Number() turns text into a number. For text that is not a number it gives NaN, "Not a Number", which is still of type number:
console.log(Number("abc"));NaNAlways check results that come from text.
7. A typo in a Math helper
console.log(Math.rond(2.6));Node prints all the lines before and then crashes:
TypeError: Math.rond is not a functiontsc finds it first and suggests the fix:
npx tsc --noEmit numbers.tsnumbers.ts:13:18 - error TS2551: Property 'rond' does not exist on type 'Math'. Did you mean 'round'?Correct the name to Math.round(2.6): tsc prints nothing, and the program runs to the end (the last line prints 3).
Final code
const apples = 7;
const people = 3;
console.log(apples + people);
console.log(apples - people);
console.log(apples * people);
console.log(apples / people);
console.log(apples % people);
console.log(2 ** 10);
console.log(Math.round(apples / people));
console.log(Math.max(4, 9, 2));
console.log(0.1 + 0.2);
console.log(Number("abc"));
console.log(Math.round(2.6));10
4
21
2.3333333333333335
1
1024
2
9
0.30000000000000004
NaN
3Recap
- One type,
number; operators+ - * / % **. Math.round,Math.maxand friends; the decimal surprise;NaN.
Next episode: Strings.