You are here

Java arithmetic operators are trickey see how you can over come hurdles

Java script arithmetic operators are trickey see why, If the operands are numbers, regular arithmetic multiplication is performed, meaning that two positives or two negatives equal a positive, whereas operands with different signs yield a negative. If the result cannot be represented in JavaScript, either Infinity or -Infinity is returned.

  1. If either operand is NaN, the result is NaN.
  2. If Infinity is multiplied by 0, the result is NaN.
  3. If Infinity is multiplied by any number other than 0, the result is either Infinity or -Infinity, depending on the sign of the second operand.
  4. If Infinity is multiplied by Infinity, the result is Infinity.
  5. If the operands are numbers, regular arithmetic division is performed, meaning that two positives or two negatives equal a positive, whereas operands with different signs yield a negative. If the result can't be represented in JavaScript, it returns either Infinity or -Infinity.
  6. If Infinity is divided by Infinity, the result is NaN.
  7. If Infinity is divided by any number, the result is Infinity.
  8. If zero is divided by zero, the result is NaN.
  9. If a nonzero finite number is divided by zero, the result is either Infinity or -Infinity, depending on the sign of the first operand.
  10. If Infinity is divided by any number other than zero, the result is either Infinity or -Infinity, depending on the sign of the second operand.
  11. If the operands are numbers, regular arithmetic division is performed, and the remainder of that division is returned.
  12. If the dividend is Infinity or the divisor is 0, the result is NaN.
  13. If the divisor is an infinite number, the result is the dividend.
  14. If the dividend is zero, the result is zero.
  15. If either operand isn't a number, it is converted to a number behind the scenes using Number() and then the other rules are applied.
  16. If the two operands are numbers, perform arithmetic subtract and return the result.
  17. If Infinity is subtracted from Infinity, the result is NaN.
  18. If -Infinity is subtracted from -Infinity, the result is NaN.
  19. If -Infinity is subtracted from Infinity, the result is Infinity.
  20. If Infinity is subtracted from -Infinity, the result is -Infinity.
  21. If +0 is subtracted from +0, the result is +0.
  22. If −0 is subtracted from +0, the result is −0.
  23. If −0 is subtracted from −0, the result is +0.
  24. If either operand is a string, a Boolean, null, or undefined, it is converted to a number (using Number() behind the scenes) and the arithmetic is calculated using the previous rules. If that conversion results in NaN, then the result of the subtraction is NaN.
  25. If either operand is an object, its valueOf() method is called to retrieve a numeric value to represent it. If that value is NaN, then the result of the subtraction is NaN. If the object doesn't have valueOf() defined, then toString() is called and the resulting string is converted into a number.
  26. Unary operators

  • When used on a string that is not a valid number, the variable's value is set to NaN. The variable is changed from a string to a number.
  • When used on a Boolean value that is false, convert to 0 and apply the change. The variable is changed from a Boolean to a number.
  • When used on a Boolean value that is true, convert to 1 and apply the change. The variable is changed from a Boolean to a number.
  • When used on a floating-point value, apply the change by adding or subtracting 1.
  • When used on an object, call its valueOf() method to get a value to work with. Apply the other rules. If the result is NaN, then call toString() and apply the other rules again. The variable is changed from an object to a number.

Boolean operators

OR

  • If the first operand is an object, then the first operand is returned.
  • If the first operand evaluates to false, then the second operand is returned.
  • If both operands are objects, then the first operand is returned.
  • If both operands are null, then null is returned.
  • If both operands are NaN, then NaN is returned.
  • If both operands are undefined, then undefined is returned.

AND

  • If the first operand is an object, then the second operand is always returned.
  • If the second operand is an object, then the object is returned only if the first operand evaluates to true.
  • If both operands are objects, then the second operand is returned.
  • If either operand is null, then null is returned.
  • If either operand is NaN, then NaN is returned.
  • If either operand is undefined, then undefined is returned.

NOT

  • If the operand is an object, false is returned.
  • If the operand is an empty string, true is returned.
  • If the operand is a nonempty string, false is returned.
  • If the operand is the number 0, true is returned.
  • If the operand is any number other than 0 (including Infinity), false is returned.
  • If the operand is null, true is returned.
  • If the operand is NaN, true is returned.
  • If the operand is undefined, true is returned.