- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
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 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.
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.
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.
Below example will show you that how you can use the subtraction operator to subtract the corresponding elements of two different arrays −
Kickstart Your Career
Get certified by completing the courseGet Started