How to Subtract One Number from Another with JavaScript

Posted by

To subtract one number from another in JavaScript, you can use the subtraction operator -.

Here’s an example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 10;
let b = 5;
let c = a - b;

console.log(c);  // Output: 5

[/dm_code_snippet]

In this example, the value of a is 10 and the value of b is 5. When we subtract b from a, we get a result of 5, which is stored in the variable c.

You can also use the - operator to subtract a number from a variable that has already been declared:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 10;
x = x - 5;

console.log(x);  // Output: 5

[/dm_code_snippet]

In this example, the value of x is initially set to 10. We then use the assignment operator = to reassign the value of x to the result of x - 5. This subtracts 5 from the original value of x, leaving a final value of 5.

It’s important to note that the - operator can only be used to subtract numbers. If you try to subtract a string or a boolean value from a number, you will get an error.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let y = 10;
let z = "5";
let result = y - z;  // This will produce an error

[/dm_code_snippet]

In this case, z is a string, not a number. JavaScript will not be able to perform the subtraction operation and will throw an error. To avoid this, you can use the parseInt() or Number() function to convert the string to a number before performing the subtraction.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let y = 10;
let z = "5";
let result = y - Number(z);  // This will work

console.log(result);  // Output: 5

[/dm_code_snippet]

Here are a few more examples of how to use the subtraction operator in JavaScript:

  • Subtracting decimals:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 10.5;
let b = 5.2;
let c = a - b;

console.log(c);  // Output: 5.3

[/dm_code_snippet]

  • Subtracting negative numbers:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = -10;
let b = -5;
let c = a - b;

console.log(c);  // Output: -5

[/dm_code_snippet]

  • Subtracting a mix of positive and negative numbers:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 10;
let b = -5;
let c = a - b;

console.log(c);  // Output: 15

[/dm_code_snippet]

It’s also worth noting that the subtraction operator - has a lower precedence than most other operators in JavaScript. This means that it will be evaluated after most other operations have been performed.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 10;
let b = 5;
let c = 2;
let d = a - b * c;

console.log(d);  // Output: 0

[/dm_code_snippet]

In this example, the multiplication b * c is performed first, resulting in a value of 10. This value is then subtracted from a, resulting in a final value of 0.

If you want to subtract b from a first, you can use parentheses to specify the order in which the operations should be performed:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let a = 10;
let b = 5;
let c = 2;
let d = (a - b) * c;

console.log(d);  // Output: 10

[/dm_code_snippet]