Subtracting Numbers in TypeScript

Subtracting Numbers in TypeScript

Subtraction is one of the most basic ideas in programming, yet it is something you will use almost every day as a developer. Any time you need to find a difference between values, reduce a total, track remaining items, or calculate change, subtraction plays an important role. In TypeScript, subtracting numbers is simple and very close to how we think about math in real life, which makes it perfect for beginners.

TypeScript is popular because it adds safety and clarity on top of JavaScript. When you subtract numbers in TypeScript, the language helps you avoid common mistakes by making sure you are working with the correct data types. Learning how subtraction works early will help you feel confident when building web apps, handling user input, or performing calculations in real projects.

Program 1: Subtracting Two Integer Numbers

This program shows the simplest way to subtract one whole number from another in TypeScript. The values are already defined so you can focus on understanding the subtraction itself.

let totalApples: number = 20;
let eatenApples: number = 7;

let remainingApples: number = totalApples - eatenApples;
console.log(remainingApples);

In this example, two integer values are stored in variables and subtracted using the minus operator. The result is saved in a new variable and printed to the console. This is useful for basic calculations like counting remaining items, tracking scores, or reducing a value step by step.

Program 2: Subtracting Decimal Numbers

This program demonstrates subtraction using numbers with decimal points, which is common in everyday calculations.

let accountBalance: number = 150.75;
let withdrawalAmount: number = 45.25;

let newBalance: number = accountBalance - withdrawalAmount;
console.log(newBalance);

Here, both values are decimal numbers, and TypeScript handles them without any extra work. This kind of subtraction is useful in financial apps, shopping carts, and measurement-based programs. Beginners can see that subtraction works the same way for decimals as it does for whole numbers.

Program 3: Subtracting Mixed Numbers

This example shows how TypeScript subtracts a decimal number from a whole number.

let distancePlanned: number = 100;
let distanceCovered: number = 42.5;

let distanceLeft: number = distancePlanned - distanceCovered;
console.log(distanceLeft);

Even though the numbers are different in form, TypeScript treats them both as numbers and performs the subtraction correctly. This is helpful in real-world scenarios like tracking progress, remaining time, or leftover quantities. Beginners can learn that TypeScript keeps calculations simple and predictable.

Program 4: Subtracting Numbers Using a Function

This program uses a function to subtract two numbers, which is a clean and reusable approach.

function subtractNumbers(startValue: number, subtractValue: number): number {
    return startValue - subtractValue;
}

let result = subtractNumbers(50, 18);
console.log(result);

Using a function allows you to reuse the subtraction logic wherever you need it. This makes your code easier to read and maintain as your projects grow. Beginners can start building good habits by learning how to wrap simple operations inside functions.

Program 5: Subtracting Numbers from User Input

This program shows how to subtract numbers entered by the user.

let firstInput = prompt("Enter the first number:");
let secondInput = prompt("Enter the number to subtract:");

let firstNumber: number = Number(firstInput);
let secondNumber: number = Number(secondInput);

let difference: number = firstNumber - secondNumber;
console.log(difference);

User input is usually received as text, so it is converted into numbers before subtraction. This step is very important to avoid incorrect results. Beginners will find this example helpful for understanding how real users interact with programs and how TypeScript handles input safely.

Program 6: Subtracting Values Stored in an Array

This example subtracts one value from another using numbers stored in an array.

let scores: number[] = [85, 30];

let finalScore: number = scores[0] - scores[1];
console.log(finalScore);

Arrays are commonly used to store multiple values together. This program shows how to access array elements and subtract them. Beginners can apply this idea when working with lists of values such as scores, prices, or measurements.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about subtracting numbers in TypeScript.

Q1. Can I subtract numbers without declaring their type?
Yes, TypeScript can infer types, but declaring them helps beginners write clearer and safer code.

Q2. Does subtraction work the same for integers and decimals?
Yes, TypeScript handles both in the same way as long as they are numbers.

Q3. Why do I need to convert user input before subtraction?
User input is read as text, and converting it ensures that subtraction works correctly.

Q4. What happens if I subtract a bigger number from a smaller one?
TypeScript will return a negative number, which is completely valid in many calculations.

Q5. Is subtraction in TypeScript different from JavaScript?
The syntax is the same, but TypeScript adds type checking to reduce errors.

Conclusion

Subtracting numbers in TypeScript is simple, clear, and beginner-friendly. In this guide, you explored subtraction with integers, decimals, mixed values, arrays, functions, and user input. Each example showed a practical way to find differences using clean and readable TypeScript code.

The best way to improve is to practice. Try changing the numbers, writing your own subtraction functions, or combining subtraction with other operations. As you get comfortable with these basics, you will find it much easier to build real TypeScript applications with confidence.

Scroll to Top