number type
- TypeScript course
- Beginner
- Lesson 7/89
- 4 min
Learn to define numbers with the number type.
What is the number type?
The number type holds both integers and decimals; TypeScript has no separate int or float type. The type annotation again uses a colon.
let yas: number = 25;
let fiyat: number = 19.99;
console.log(yas, fiyat); // 25 19.99Whether you write 5 or 5.5, both are accepted as number.
Arithmetic with number
You can perform addition (+), subtraction (-), multiplication (*) and division (/) on number values. The result is again of type number.
let a: number = 12;
let b: number = 4;
console.log(a + b); // 16
console.log(a / b); // 3Practice Declare two numbers and print them
Declare a number variable named yas with value 30. Declare a second number named boy with value 1.75. Print both with a single console.log.
Starter code
// yas ve boy adında iki number değişken tanımla
// ikisini tek console.log ile yazdırSample solution
let yas: number = 30;
let boy: number = 1.75;
console.log(yas, boy);Expected output
30 1.75
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which of the following assignments is valid?
- let fiyat: number = "19.99";
- let fiyat: int = 19.99;
- let fiyat: float = 19.99;
- let fiyat: number = 19.99; Correct answer
Explanation: TypeScript uses the single number type for both integers and decimals; there is no int or float. A text value cannot be assigned to number.
2 Complete the type annotation
let sicaklik: ____ = 23.5;
let sicaklik: number = 23.5;
Explanation: TypeScript has no separate type for integers and decimals; both use number.
3 number concepts
| Term | Meaning |
|---|---|
number |
integer and decimal type |
25 |
integer value |
19.99 |
decimal value |
: number |
type name |