What is Subtraction Operator (-) in JavaScript?


The subtraction operator is also known as the minus operator. It is a binary operator that means it requires at least two operands to operate on. It will not work with a single operand. Generally, the subtraction operator subtracts or minus the value of the second operand from the first operand and returns the result as it is, that means if the result of two numbers subtraction is negative it will return the negative result, else it will return positive result if that’s positive.

In this article, we are going to discuss about the subtraction operator in details and its working in different scenarios with examples in JavaScript.

Syntax

The following syntax will show how you can use the subtraction operator while coding in JavaScript −

var subtract = operand1 - operand2;

How does the subtraction operator work?

The below example will illustrate the working of the subtraction operator with two operands −

9 – 5 = 4 // Answer is positive 4, as operand1 is greater than operand2
5 - 9 = -4 // Answer is negative 4, because operand1 is less than operand2

Let us understand the working of the subtraction operator in detail by practically implementing it inside the code example.

Algorithm

Step 1 − In the first step of the algorithm, we will add two different input elements in the HTML document to get the numbers input from the user.

Step 2 − In this step, we will add a button tag with an onclick event associated with it, which will later call a function once the event is triggered by the user on clicking the button.

Step 3 − In the next step, we will define a JavaScript function that will lead in showing the results to the user by clicking the button as it will be passed to the onclick event as its value.

Step 4 − In the last step, we will grab the values entered by the user inside the input tags and then operate the subtraction operator on those values to show the working of the subtraction operator to the user.

Example

The below example will explain how you can practically implement the subtraction operator inside the JavaScript code −

<html>
   <body>
      <h2> Working with Subtraction Operator (-) in JavaScript</h2>
      <p> Enter any two numbers: </p>
      <lable>num1:</label><input type = "number" id = "inp1" > <br> <br>
      <lable>num2:</label><input type = "number" id = "inp2" > <br><br>
      <button onclick = "display()"> Click to See Results </button><br>
      <p id = "result"> </p>
      <script>
         var result = document.getElementById("result");
         function display() {
            var inp1 = document.getElementById("inp1");
            var inp2 = document.getElementById("inp2");
            var num1 = Number(inp1.value);
            var num2 = Number(inp2.value);
            var ans1 = num1 - num2;
            var ans2 = num2 - num1;
            result.innerHTML += 'num1 - num2 : ' + ans1 + "<br>";
            result.innerHTML += 'num2 - num1 : ' + ans2 + " <br> ";
         }
      </script>
   </body>
</html>

In this example, we have taken two numbers input from the user and then grab their values using JavaScript. After that we use the subtraction operator to subtract the values from each other.

Let us consider one more code example, where we will use the subtraction operator in solving the real time problem by subtracting the corresponding elements of two different arrays.

Algorithm

Step 1 − In the first step, we will consider two different statically typed JavaScript arrays with some initial values associated with them.

Step 2 − In this step, we will use the for loop to subtract the corresponding elements of both the arrays and store the result at the same position in a new array.

Step 3 − In the last step, we will display the result of the subtraction or the new array we get after subtracting the corresponding elements of two arrays.

Example

Below example will show you that how you can use the subtraction operator to subtract the corresponding elements of two different arrays −

<html>
   <body>
      <h2> Subtraction Operator (-) in JavaScript </h2>
      <p> Initial Arrays: <br>
         Array1: <b> [11, 12, 13, 14, 15] </b> <br>
         Array2: <b> [2, 4, 6, 8, 10] </b>
      </p>
      <button onclick = "display()"> Click to See Results </button>
      <p id = "result"> </p>
      <script>
         var result = document.getElementById("result");
         var Array1 = [11, 12, 13, 14, 15];
         var Array2 = [2, 4, 6, 8, 10];
         var answer = [];
         for (var i = 0; i < 5; i++) {
            answer[i] = Array1[i] - Array2[i];
         }
         function display() {
            result.innerHTML += " Array1 - Array2 is: <b> ]" + answer + "] </b> <br> ";
         }
      </script>
   </body>
</html>

In the above example, we have taken two static JavaScript arrays of length = 5, with different elements and then used the for loop to subtract the corresponding elements values using the subtraction operator, which results in a good application of subtraction operator.

In this article, we have learnt about the subtraction operator in JavaScript. We have discussed the subtraction operator in details with the help of code example by implementing it practically inside them. In the first code example, we have seen the simple and the naïve application of the subtraction operator for subtracting two different numbers from each other, while in the second example, we have discussed the real time use of the subtraction operator in which we wre using it to solve the problems related to the subtraction of the corresponding elements of two different arrays.

Updated on: 06-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements