![]() |
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 Table of ContentUsing the Negative Operator ( – ) in ConsoleWe 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
Output
-10 10 10 -10 10 0 Using User Defined FunctionsFunctions 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) { Example: This example describes the subtraction of 2 numbers using a Function. Javascript
Output
after subtraction : -10 Using Arrow FunctionThe 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) => { Example: This example describes the subtraction of 2 numbers using the Arrow function. Javascript
Output
after subtraction : -10 Using the Subtract Assignment OperatorIt 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
Output
10 |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |