JavaScript Program to Subtract Two Numbers
Last Updated :
22 Sep, 2023
In this article, we will see how to subtract two numbers in JavaScript. A Basic arithmetic operation known as Subtraction involves taking one number (the "subtrahend") away from another (the "minuend") in order to calculate their difference. It is denoted by the minus sign (-). A subtraction's outcome is referred to as the "difference."
Syntax:
Result = Minuend - Subtrahend
Let's take two numbers:
Input: a = 10 , b = 20
Output:
b - a = 20 - 10 = 10
a - b = 10 - 20 = -10
Using the Negative Operator ( - ) in Console
We can directly perform Subtraction operations in the console by using the "console.log()" statement by providing two operands that will print the exact difference value.
Example: This example describes the subtraction of two numbers using "console.log".
JavaScript
let a = 10;
let b = 20;
// Subtract directlty
console.log(a - b); // -10
console.log(b - a); // 10
// Subtract by using another variable
let c = b - a;
console.log(c); // 10
// Subtract without any variable
console.log(10 - 20); //-10
console.log(20 - 10); //10
console.log(20 - 10 - 10); //0
Using User Defined Functions
Functions in Javascript can be used to calculate the difference between the 2 operands. The function will take parameters as 2 numbers, then the function will return the subtraction of the input parameters.
Syntax:
function fun_name (param1 , param2) {
return param1 - param2 ;
}
Example: This example describes the subtraction of 2 numbers using a Function.
JavaScript
// First variable
let a = 10;
// Second variable
let b = 20;
// Funciton to subtract two variables
function subtract(a, b) {
return a - b;
}
// For printing the result
console.log("after subtraction : "
+ subtract(a, b));
Outputafter subtraction : -10
Using Arrow Function
The subtraction of two numbers can also be done using the arrow function, where the 2 variables are passed as a parameter of that function and return the value after subtraction
Syntax:
let var_name = (param) => {
// code
}
Example: This example describes the subtraction of 2 numbers using the Arrow function.
JavaScript
// First variable
let a = 10;
// Second variable
let b = 20;
// Arrow funciton for subtraction of two numbers
let ans = (a, b) => a - b;
// For printing the result in console
console.log("after subtraction : " + ans(a, b));
Outputafter subtraction : -10
Using the Subtract Assignment Operator
It subtracts the right operand from the left operand and assigns the result to the left operand. The value provided on the right-hand side will decide how much value it must be decreased.
Syntax:
var_name -= value;
Example: This example describes the subtraction of 2 numbers using the subtraction AND assignment operator.
JavaScript
// Create a variable
let a = 20;
let b = 10;
a -= b;
// Print the result
console.log(a); // 10
Similar Reads
JavaScript Program for Subtraction of Matrices Subtraction of two matrices is a basic mathematical operation that is used to find the difference between the two matrices. In this article, we will see how we can perform the subtraction of input matrices using JavaScript. Example: Table of ContentUsing Loop in JavaScriptUsing the map method in Jav
5 min read
JavaScript Program to Perform Simple Mathematical Calculation In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 :
4 min read
Javascript Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no
5 min read
Subtraction(-) Arithmetic Operator in JavaScript JavaSscript arithmetic subtraction operator is used to find the difference between operators after subtracting them. Depending on the nature of the two operands, the subtraction operator performs either number subtraction or BigInt subtraction after converting both operands to numeric values. Syntax
1 min read
StrictMath subtractExact() in Java With Examples subtractExact(int num1, int num2) The subtractExact(int num1, int num2) is an inbuilt method of StrictMath class in java which is used to get the difference of the given arguments num1 and num2. If the result overflows int, it throws an exception. Since subtractExact(int num1, int num2) is static, s
3 min read
BigInteger subtract() Method in Java with Examples The java.math.BigInteger.subtract(BigInteger val) is used to calculate the Arithmetic difference of two BigIntegers. This method is applicable on large value numbers of range much greater than the range of biggest data type double of Java without compromising with the precision of the result but as
3 min read