JavaScript: Subtract Two Numbers

1. Introduction

Subtraction is one of the basic arithmetic operations that we learned in our early school days. Today, we'll see how to perform subtraction using JavaScript, a versatile and widely used programming language.

2. Program Overview

In this guide, we'll walk through a simple JavaScript program that:

1. Declares two variables containing numbers.

2. Subtract the second number from the first.

3. Outputs the result.

3. Code Program

let num1 = 10;     // First number
let num2 = 3;      // Second number to be subtracted from the first
let difference = num1 - num2;   // Subtracting num2 from num1
console.log("The difference between " + num1 + " and " + num2 + " is: " + difference);


The difference between 10 and 3 is: 7

4. Step By Step Explanation

1. Variable Declaration: We start by declaring two variables, num1, and num2, which are initialized with values 10 and 3 respectively. These variables will hold the numbers we aim to subtract.

2. Subtraction Operation: Using the statement let difference = num1 - num2;, we subtract the value stored in num2 from num1. The resultant difference is then stored in the difference variable.

3. Displaying the Result: The final step involves using the console.log function to display our result. This involves a concatenated string that describes the operation and the result, making it clear to the end user.