![]() |
VOOZH | about |
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."
Result = Minuend - SubtrahendLet's take two numbers:
Input: a = 10 , b = 20
Output:
b - a = 20 - 10 = 10
a - b = 10 - 20 = -10
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".
-10 10 10 -10 10 0
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.
function fun_name (param1 , param2) {
return param1 - param2 ;
}
Example: This example describes the subtraction of 2 numbers using a Function.
after subtraction : -10
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
let var_name = (param) => {
// code
}
Example: This example describes the subtraction of 2 numbers using the Arrow function.
after subtraction : -10
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.
var_name -= value;Example: This example describes the subtraction of 2 numbers using the subtraction AND assignment operator.
10