JavaScript basic learning series twenty-three: subtraction operator

Like the addition operator, the subtraction operator has a set of rules for handling conversions between different types in ECMAScript.

  • If both operands are numeric, a mathematical subtraction operation is performed and the result is returned.
  • If either operand is NaN, NaN is returned.
  • If it is Infinity minus Infinity, NaN is returned.
  • If -Infinity minus -Infinity, returns NaN.
  • If it is Infinity minus -Infinity, Infinity is returned.
  • If -Infinity minus Infinity, -Infinity is returned.
  • If it is +0 minus +0, +0 is returned. 7
  • If it is +0 minus -0, then -0 is returned.
  • If it is -0 minus -0, +0 is returned.
  • If any of the operands is a string, Boolean, null, or undefined, it is first converted to a numeric value using Number() behind the scenes, and then the mathematical operation is performed according to the previous rules. If the conversion result is NaN, the result of the subtraction calculation is NaN.
  • If any operand is an object, its valueOf() method is called to obtain the value representing it. If the value is NaN, the result of the subtraction calculation is NaN. If the object does not have a valueOf() method, its toString() method is called, and then the resulting string is converted to a numeric value.

let result1 = 5 - true; // true被转换为1,所以结果是4 let result2 = NaN - 1; // NaN
let result3=5-3; let result4 = 5 - ""; let result5 = 5 - "2"; let result6 = 5 - null;
//2
// ""被转换为0,所以结果是5 // "2"被转换为2,所以结果是3 // null被转换为0,所以结果是5

1. Relational operators:

Relational operators perform operations to compare two values, including less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=). 13 The usage is the same as what you learned in math class.

Like other operators in ECMAScript, type conversions and other behavior occur when they are applied to different data types.

  • If the operands are both numeric, a numeric comparison is performed.
  • If the operands are both strings, the encodings of the corresponding characters in the strings are compared one by one.
  • If either operand is a numeric value, the other operand is converted to a numeric value and a numeric comparison is performed.
  • If any operand is an object, its valueOf() method is called, and after obtaining the result, the comparison is performed according to the previous rules. If there is no valueOf() operator, the toString() method is called, and after obtaining the result, the comparison is performed according to the previous rules.
  • If either operand is a Boolean value, it is converted to a numeric value and the comparison is performed.

An interesting phenomenon occurs when comparing two strings using relational operators. Many people think that less than means "alphabetically earlier" and greater than means "alphabetically later", but this is not actually the case. For strings, relational operators compare the encodings of corresponding characters in the string, and these encodings are numeric. After comparison, a Boolean value will be returned. The crux of the matter is that the encoding for uppercase letters is smaller than the encoding for lowercase letters.

Guess you like

Origin blog.csdn.net/wanmeijuhao/article/details/135450119