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.
number type for both whole numbers
and decimal numbers. You do not need separate types such as
int or float.
The number type represents numeric values in TypeScript.
let age: number = 20;
Here, age is a variable of type number.
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.
You can use const when the numeric variable should not be
reassigned.
const age: number = 25;
console.log(age);
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.
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);
Negative numbers can also be stored using the number type.
let temperature: number = -5;
let balance: number = -500;
console.log(temperature);
console.log(balance);
Zero is also a valid number.
let count: number = 0;
console.log(count);
The value 0 has the type number.
TypeScript can automatically determine the type of a numeric variable.
let age = 20;
TypeScript infers that age is a
number.
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.
A variable declared as number cannot receive a string.
let age: number = 20;
// Error
// age = "twenty";
TypeScript checks the type before allowing the assignment.
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
The - operator is used for subtraction.
let a: number = 50;
let b: number = 20;
let result: number = a - b;
console.log(result);
Output:
30
The * operator is used for multiplication.
let price: number = 100;
let quantity: number = 5;
let total: number = price * quantity;
console.log(total);
Output:
500
The / operator is used for division.
let total: number = 100;
let students: number = 4;
let result: number =
total / students;
console.log(result);
Output:
25
The % operator returns the remainder after division.
let result: number = 10 % 3;
console.log(result);
Output:
1
The ++ operator increases a number by one.
let count: number = 5;
count++;
console.log(count);
Output:
6
The -- operator decreases a number by one.
let count: number = 5;
count--;
console.log(count);
Output:
4
Numbers can be compared using comparison operators.
let age: number = 20;
console.log(age >= 18);
Output:
true
Comparison expressions produce a boolean result.
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
The Math.min() method returns the smallest number.
let result: number =
Math.min(10, 50, 30);
console.log(result);
Output:
10
The Math.round() method rounds a number to the nearest
whole number.
let result: number =
Math.round(10.6);
console.log(result);
Output:
11
The Math.floor() method rounds a number down to the
nearest integer.
let result: number =
Math.floor(10.9);
console.log(result);
Output:
10
The Math.ceil() method rounds a number up to the nearest
integer.
let result: number =
Math.ceil(10.1);
console.log(result);
Output:
11
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.
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);
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.
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.
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.
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.
| 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 |
number type for whole numbers,
decimal numbers, positive numbers, negative numbers, and mathematical
calculations.
number type is used for numeric values.number for both integers and decimals.: number.+ operator performs addition between numbers.- operator performs subtraction.* operator performs multiplication./ operator performs division.Question: Which TypeScript type is used for both whole numbers and decimal numbers?