The subtraction operator (-) subtracts the number to the right of the operator from the number to the left.
If either operand is a string, an attempt is made to convert strings to numbers. Instead of using "5" if you try console.log ("abc" - 1); it will cause an error like NaN.
For information only: The subtraction operator has special rules for handling the variety of type conversions present in JavaScript:
If the two operands are numbers, do arithmetic subtraction and return the result.
If any number is NaN, the result is NaN.
If infinity is subtracted from infinity, the result is NaN.
If -Infinity is subtracted from -Infinity, the result is NaN.
If -Infinity is subtracted from infinity, the result is infinity.
If Infinity is subtracted from -Infinity, the result is -Infinity.
If +0 is subtracted from +0, the result is +0.
If -0 is subtracted from +0, the result is -0.
If -0 is subtracted from -0, the result is +0.
If either of the two operands is not a number, the result is NaN.
Ravindra
source share