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>
Rules for subtract operator(Operand 1 - Operand 2 = Result):
Operand 1 | Operand 2 | Result |
---|---|---|
number | number | arithmetic subtract |
NaN | a number | NaN |
a number | NaN | NaN |
Infinity | Infinity | NaN |
-Infinity | -Infinity | NaN |
Infinity | -Infinity | Infinity |
-Infinity | Infinity | -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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator