Lesson 13 of 60 – Number Type
22%

Number Type in TypeScript

The number type is used to store numeric values in TypeScript. It can be used for integers, decimal numbers, positive numbers, negative numbers, and many mathematical calculations.

Note: TypeScript uses the number type for both whole numbers and decimal numbers. You do not need separate types such as int or float.

1. What is the Number Type?

The number type represents numeric values in TypeScript.

let age: number = 20;

Here, age is a variable of type number.

2. Declaring a Number Variable

You can declare a number variable using let.

let marks: number = 85;

console.log(marks);

The type annotation : number tells TypeScript that the variable should contain a numeric value.

3. Number with const

You can use const when the numeric variable should not be reassigned.

const age: number = 25;

console.log(age);

4. Integer Numbers

Whole numbers without decimal points are also represented by the number type.

let students: number = 50;

let marks: number = 90;

let year: number = 2026;

All of these values have the type number.

5. Decimal Numbers

TypeScript uses the same number type for decimal values.

let price: number = 499.99;

let percentage: number = 85.5;

console.log(price);
console.log(percentage);

6. Negative Numbers

Negative numbers can also be stored using the number type.

let temperature: number = -5;

let balance: number = -500;

console.log(temperature);
console.log(balance);

7. Zero

Zero is also a valid number.

let count: number = 0;

console.log(count);

The value 0 has the type number.

8. Number Type Inference

TypeScript can automatically determine the type of a numeric variable.

let age = 20;

TypeScript infers that age is a number.

9. Changing a Number Variable

A number variable declared using let can be reassigned.

let score: number = 50;

score = 75;

score = 90;

console.log(score);

The final value of score is 90.

10. Number Cannot Receive a String

A variable declared as number cannot receive a string.

let age: number = 20;

// Error
// age = "twenty";

TypeScript checks the type before allowing the assignment.

11. Addition with Numbers

The + operator can be used to add numbers.

let a: number = 10;
let b: number = 20;

let result: number = a + b;

console.log(result);

Output:

30

12. Subtraction with Numbers

The - operator is used for subtraction.

let a: number = 50;
let b: number = 20;

let result: number = a - b;

console.log(result);

Output:

30

13. Multiplication with Numbers

The * operator is used for multiplication.

let price: number = 100;
let quantity: number = 5;

let total: number = price * quantity;

console.log(total);

Output:

500

14. Division with Numbers

The / operator is used for division.

let total: number = 100;
let students: number = 4;

let result: number =
    total / students;

console.log(result);

Output:

25

15. Remainder Operator

The % operator returns the remainder after division.

let result: number = 10 % 3;

console.log(result);

Output:

1

16. Increment Operator

The ++ operator increases a number by one.

let count: number = 5;

count++;

console.log(count);

Output:

6

17. Decrement Operator

The -- operator decreases a number by one.

let count: number = 5;

count--;

console.log(count);

Output:

4

18. Comparing Numbers

Numbers can be compared using comparison operators.

let age: number = 20;

console.log(age >= 18);

Output:

true

Comparison expressions produce a boolean result.

19. Math.max()

The Math.max() method returns the largest number from the provided values.

let result: number =
    Math.max(10, 50, 30);

console.log(result);

Output:

50

20. Math.min()

The Math.min() method returns the smallest number.

let result: number =
    Math.min(10, 50, 30);

console.log(result);

Output:

10

21. Math.round()

The Math.round() method rounds a number to the nearest whole number.

let result: number =
    Math.round(10.6);

console.log(result);

Output:

11

22. Math.floor()

The Math.floor() method rounds a number down to the nearest integer.

let result: number =
    Math.floor(10.9);

console.log(result);

Output:

10

23. Math.ceil()

The Math.ceil() method rounds a number up to the nearest integer.

let result: number =
    Math.ceil(10.1);

console.log(result);

Output:

11

24. Numbers in Functions

Functions can accept numbers as parameters and return numbers.

function add(
    a: number,
    b: number
): number {

    return a + b;
}

console.log(
    add(10, 20)
);

The parameters and return value are explicitly typed as numbers.

25. Numbers in Objects

Number values can be stored as object properties.

let student: {
    name: string;
    age: number;
    marks: number;
} = {

    name: "Rahul",
    age: 20,
    marks: 85

};

console.log(student.age);
console.log(student.marks);

26. Number Arrays

You can create an array containing numbers using number[].

let marks: number[] = [
    80,
    75,
    90,
    85
];

console.log(marks);

Every element in this array is a number.

27. Numbers in React Native

Numbers are commonly used in React Native applications for prices, quantities, counters, dimensions, scores, and other numeric values.

const price: number = 500;
const quantity: number = 2;

const total: number =
    price * quantity;

console.log(total);

The calculated total can then be used by the application.

28. Numbers with React Native Style Values

React Native style properties commonly use numeric values for dimensions such as width, height, margin, padding, and font size.

const boxWidth: number = 200;
const boxHeight: number = 100;
const fontSize: number = 20;

These values can be used when defining React Native styles.

29. Common Number Type Mistakes

Mistake 1: Assigning a string to a number variable.

let age: number = 20;

// Error
// age = "twenty";

Mistake 2: Using a string method on a number.

let price: number = 500;

// Error
// price.toUpperCase();

Use operations and methods that are appropriate for the value's type.

30. Number Type Summary

Feature Example
Number annotation let age: number = 20;
Number inference let age = 20;
Decimal number let price: number = 99.50;
Addition a + b
Subtraction a - b
Multiplication a * b
Division a / b
Remainder a % b
Remember: TypeScript uses the number type for whole numbers, decimal numbers, positive numbers, negative numbers, and mathematical calculations.

📌 Key Points

  • The number type is used for numeric values.
  • TypeScript uses number for both integers and decimals.
  • Positive, negative, and zero values are all numbers.
  • TypeScript can infer the number type automatically.
  • You can explicitly use : number.
  • Number variables can be used with mathematical operators.
  • The + operator performs addition between numbers.
  • The - operator performs subtraction.
  • The * operator performs multiplication.
  • The / operator performs division.
  • Numbers are widely used in React and React Native applications.

🧠 Quick Quiz

Question: Which TypeScript type is used for both whole numbers and decimal numbers?