• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Creating a JavaScript Function to Subtract Two Numbers

Creating a JavaScript Function to Subtract Two Numbers

September 9, 2022 Leave a Comment

We can use JavaScript to create a function that will subtract two numbers easily. We simply need to use the subtraction operator – in our function. Here is the simple function to subtract two numbers.

function subtractTwoNumbers(number1,number2){
  return number1-number2;
};

And that’s it.

Our simple function called subtractTwoNumbers takes two parameters, the two numbers you want to subtract. The function just subtracts the second number from the first and then returns the result.

Let’s see some examples of this function in use and the output it produces.

function subtractTwoNumbers(number1,number2){
  return number1-number2;
};

console.log(subtractTwoNumbers(2,3));
console.log(subtractTwoNumbers(-2,3));
console.log(subtractTwoNumbers(3,2));
console.log(subtractTwoNumbers(1092,6732));
console.log(subtractTwoNumbers(-22,-3));
console.log(subtractTwoNumbers(10002,0));

#Output
-1
-5
1
-5640
-19
10002

Creating a JavaScript Function to Subtract Two Numbers Using User Input

Below is an example of how we can create a function that will subtract two numbers, and then execute the function when a user clicks on a button we create. We will call the JavaScript function we create using an onclick event that we will attach to the HTML button.

<p>Enter two numbers below. Click Submit to have the subtracted value of the numbers displayed:</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="subtractNumbers()">Submit</button>
<div id="results"></div>

Below we will write our function. Because the button has the event onclick="subtractNumbers()", the subtractNumbers() function, which we will create below, will run when the button is clicked. The function will subtract the two numbers that the user enters together and display the result. If something other than a number is entered, NaN will be returned.

We will retrieve the numbers the user has entered using the value property.

function subtractNumbers() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  var total = Number(num1) - Number(num2);
  document.getElementById("results").textContent = total;    
};

The final code and output for this example is below:

Code Output:

Enter two numbers below. Click Submit to have the subtracted value of the numbers displayed:



Full Code:

<p>Enter two numbers below. Click Submit to have the subtracted value of the numbers displayed:</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="subtractNumbers()">Submit</button>
<div id="results"></div>

<script>

function subtractNumbers() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  var total = Number(num1) - Number(num2);
  document.getElementById("results").textContent = total;    
};

</script>

Hopefully this article has been useful in helping you learn how to create a JavaScript function to subtract two numbers.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if Variable is a Number
  • 2.  JavaScript String endsWith Method
  • 3.  Using JavaScript to Get a Random Number Between 1 and 20
  • 4.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 5.  Remove the Last Element From Array in JavaScript
  • 6.  Remove Special Characters From String in JavaScript
  • 7.  Get the First Child Element and Change it with JavaScript
  • 8.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 9.  Subtract All Numbers in an Array Using JavaScript
  • 10.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy