Subtract

The subtract operator is -.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var result = 2 - 1; 
        document.writeln(result); //1
               
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Rules for subtract operator(Operand 1 - Operand 2 = Result):

Operand 1Operand 2Result
numbernumberarithmetic subtract
NaNa numberNaN
a numberNaNNaN
InfinityInfinityNaN
-Infinity-InfinityNaN
Infinity-InfinityInfinity
-InfinityInfinity-Infinity
+0+0+0
+0-0-0
-0-0+0

If either operand is a string, a Boolean, null, or undefined, it is converted to a number using Number(). If either operand is an object, its valueOf() method is called to retrieve a numeric value. If the object doesn't have valueOf() defined, then toString() is called and the resulting string is converted into a number.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        document.writeln(2 - true); //1, true is converted to 1 
        document.writeln(NaN - 1);  //NaN 
        document.writeln(2 - 3);    // -1
        document.writeln(2 - "");  //2 , "" is converted to 0 
        document.writeln(2 - "2"); //0, "2" is converted to 2 
        document.writeln(2 - null); //2, null is converted to 0     </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Operators:
  1. JavaScript Operators
  2. Increment/Decrement Operators
  3. Increment/Decrement Operators for String, Boolean, Floating-point and Object
  4. Unary Plus and Minus
  5. Bitwise Not operator
  6. Bitwise AND
  7. Bitwise OR
  8. Bitwise XOR
  9. Left Shift
  10. Signed Right Shift
  11. Unsigned Right Shift
  12. Logical NOT
  13. Logical AND
  14. Logical OR
  15. Multiply
  16. Divide
  17. Modulus
  18. Add
  19. Subtract
  20. Relational Operators
  21. Equal and Not Equal
  22. Identically Equal and Not Identically Equal
  23. Conditional Operator
  24. Assignment Operators
  25. Comma Operator