Numbers are used to do math and count things in programs. Understanding how numbers work helps you avoid mistakes.
0
0
Number type behavior in Typescript
Introduction
When you want to add, subtract, multiply, or divide values.
When you need to compare two numbers to see which is bigger.
When you want to store prices, ages, or scores.
When you want to check if a number is whole or has decimals.
When you want to convert strings to numbers for calculations.
Syntax
Typescript
let myNumber: number = 42; // You can do math like: let sum = myNumber + 8; let division = myNumber / 2;
TypeScript uses the number type for all numbers, including decimals.
Numbers can be integers (whole) or floating-point (decimals).
Examples
Store whole numbers and decimal numbers using
number type.Typescript
let age: number = 30; let price: number = 19.99;
Division can produce decimal numbers.
Typescript
let result = 10 / 4; // result is 2.5
You can use scientific notation for big or small numbers.
Typescript
let bigNumber = 1e6; // 1 million
Check if a number is whole using
Number.isInteger().Typescript
let isInteger = Number.isInteger(5); // true let isInteger2 = Number.isInteger(5.5); // false
Sample Program
This program shows basic math operations and checks if numbers are integers. It also shows a big number using scientific notation.
Typescript
let a: number = 10; let b: number = 3; console.log(`a + b = ${a + b}`); console.log(`a - b = ${a - b}`); console.log(`a * b = ${a * b}`); console.log(`a / b = ${a / b}`); console.log(`Is a integer? ${Number.isInteger(a)}`); console.log(`Is b integer? ${Number.isInteger(b)}`); let bigNum: number = 1e4; console.log(`Big number: ${bigNum}`);
OutputSuccess
Important Notes
All numbers in TypeScript are floating-point, so be careful with very precise decimal math.
Use Number.isInteger() to check if a number has no decimals.
Be aware that dividing by zero gives Infinity or -Infinity, not an error.
Summary
TypeScript uses one number type for all numbers.
Numbers can be whole or decimal, and you can do math with them easily.
Use built-in functions like Number.isInteger() to check number properties.