The number Type
Learn The number Type
TypeScript number covers JavaScript numeric values, including integers, decimals, NaN and Infinity.
In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.
What you’ll learn
- Explain the main rule behind the number type.
- Predict the result of the complete runnable TypeScript example.
- Recognize one common mistake and correct it.
Understand the concept
JavaScript uses a floating-point Number representation for ordinary numeric values. Arithmetic can produce fractions, and some decimals cannot be represented exactly in binary. Use Number.isFinite and Number.isNaN when validating runtime numeric data. Big integers use a separate bigint type.
Run a complete TypeScript example
Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.
TypeScript code
const count: number = 4;
const unitPrice: number = 12.5;
const total = count * unitPrice;
console.log(total);
console.log(Number.isFinite(total));Verified output
50
trueHow to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.
Trace the execution
The product is 50 and is finite, so the second log prints true. The number type does not separately enforce integer-only values.
Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.
Common mistakes and how to avoid them
Do not expect money calculations based on arbitrary decimal floats to be exact in every case; consider integer minor units when practical.
Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.
Practice it yourself
Change the price to 0.1 and count to 3; inspect the result and experiment with rounding for display.
Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.
Check your understanding
Review the key idea
The number Type: JavaScript uses a floating-point Number representation for ordinary numeric values.
TypeScript number follows JavaScript Number semantics.
Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.
Reaching the recap and completing the quizzes are separate milestones.