TypeScript: Subtract Two Numbers

1. Introduction

TypeScript brings the power of static typing to JavaScript, enabling developers to catch errors early and write more maintainable code. While arithmetic operations like subtraction might seem trivial, they form the foundational knowledge for any programming journey. Today, we'll explore how to create a simple TypeScript program to subtract two numbers.

2. Program Overview

The structure of our program is as follows:

1. Define a function called subtract that takes in two numbers as arguments.

2. Return the result of subtracting the second number from the first.

3. Test the function with some sample values to verify its correctness.

3. Code Program

function subtract(num1: number, num2: number): number {
    return num1 - num2;
}

console.log(`The result of 10 - 5 is: ${subtract(10, 5)}`);

Output:

The result of 10 - 5 is: 5

4. Step By Step Explanation

Our program starts by defining a subtract function that takes in two parameters, both of type number

The function simply returns the result of subtracting num2 from num1

We then test our function by calling it with the numbers 10 and 5 and printing the result to the console.

Comments