Summary of comparison rules in JavaScript (logic and arithmetic relations)

In JavaScript, looking at other people's source code, the data type is not very clear, especially the logical AND or NOR, so it is summarized as follows:

logical NOT

  • Returns false if the operand is an object
  • Returns true if the operand is an empty string
  • Returns false if the operand is a non-empty string
  • Returns true if the operand is the value 0
  • If the operand is any non-zero value (including Infinity), return false
  • Returns true if the operand is null
  • Returns true if the operand is NaN
  • Returns true if the operand is undefined

logical or

  • Returns the first operand if the first operand is an object
  • If the first operand evaluates to false, returns the second operand
  • If both operands are objects, returns the first operand
  • Returns null if both operands are null
  • Returns NaN if both operands are NaN
  • If both operands are undefined, return undefined

logic and

  • If the first operand is an object, returns the second operand
  • If the second operand is an object, it is returned only if the first operand evaluates to true
  • If both operands are objects, returns the second operand
  • Returns null if any operand is null
  • Returns NaN if any operand is NaN
  • If any operand is undefined, return undefined

multiplication

  • If the operands are all numbers, the normal multiplication calculation is performed, that is, the result of multiplying two positive numbers or two negative numbers is still a positive number, and if only one operand is signed, the result is a negative number. Returns Infinity or -Infinity if the product exceeds the expressive range of ECMAScript numeric values;
  • If any operand is NaN, the result is NaN
  • If it is Infinity multiplied by 0, then if it is NaN
  • If Infinity is multiplied by a non-zero value, the result is Infinity or -Infinity depending on the sign of the signed operand;
  • If Infinity is multiplied by Infinity, the result is Infinity;
  • If one of the operands is not a number, call Number() behind the scenes to convert it to a number, and then apply the above rules

division

  • If the operands are all numbers, the normal multiplication calculation is performed, that is, the result of multiplying two positive numbers or two negative numbers is still a positive number, and if only one operand is signed, the result is a negative number. Returns Infinity or -Infinity if the quotient exceeds the expressive range of ECMAScript values;
  • If any operand is NaN, the result is NaN
  • If Infinity is divided by Infinity, then if it is NaN
  • If zero is divided by zero, the result is NaN;
  • If a nonzero finite number is divided by zero, the result is Infinity or -Infinity, depending on the sign of the signed operand
  • If Infinity is divided by any nonzero value, the result is Infinity or -Infinity depending on the sign of the signed number.
  • If one of the operands is not a number, Number is called behind the scenes (converting it to a number before applying the rules above.

modulo

  • If the operands are all numbers, perform normal division and return the remainder
  • If the dividend is an infinite value and the divisor is a finite number, the result is NaN
  • If the dividend is a finite number and the divisor is zero, the result is NaN;
  • If Infinity is divided by Infinity, the result is NaN
  • If the dividend is a finite number and the divisor is an infinite number, the result is the dividend;
  • If the dividend is zero, the result is zero
  • If one of the operands is not a number, Number() is called behind the scenes to convert it to a number before applying the above rules.

addition

  • If any operand is NaN, the result is NaN;
  • If Infinity plus Infinity, the result is Infinity;
  • If -Infinity plus -Infinity, the result is -Infinity;
  • If Infinity plus -Infinity, the result is NaN;
  • If it is +0 plus +0, the result is +0;
  • If 0 plus 0, the result is 0;
  • If +0 plus 0, the result is +0
  • If both operands are strings, concatenate the second operand with the first;
  • If only one operand is a string, convert the other operand to a string before concatenating the two strings

subtraction

  • If it is -0 minus -0, the result is +0;
  • If one of the operands is a string, Boolean, null or undefined, the Number() function is called in the background to convert it to a numeric value, and then the subtraction calculation is performed according to the previous rules. If the result of the conversion is NaN, the result of the subtraction is NaN;
  • If one of the operands is an object, the valueOf() method of the object is called to obtain the value representing the object. If the resulting value is NaN, the result of the subtraction is NaN. If the object does not have a valueOf() method, its toString() method is called and the resulting string is converted to a value
  • If both operands are numeric, performs a normal arithmetic subtraction operation and returns the result
  • If any operand is NaN, the result is NaN;
  • If Infinity minus Infinity, the result is NaN;
  • If -Infinity minus -Infinity, the result is NaN;
  • If Infinity minus -Infinity, the result is Infinity;
  • If -Infinity minus Infinity, the result is -Infinity;
  • If it is +0 minus +0, the result is +0;
  • If +0 minus -0, the result is -0

relational operator

If both operands are numeric, a numeric comparison is performed.

  • If both operands are strings, the character encoding values ​​corresponding to the two strings are compared.
  • If one operand is a numeric value, the other operand is converted to a numeric value, and a numeric comparison is performed.
  • If one of the operands is an object, the valueOf() method of this object is called, and the comparison is performed according to the previous rules with the obtained result. If the object does not have a valueOf() method, the toString() method is called and the result is used to perform the comparison according to the preceding rules.
  • If an operand is a boolean, convert it to a numeric value before performing the comparison

equality operator

  • If one of the operands is a boolean, it is converted to a numeric value before comparing for equality—false is converted to 0, and true is converted to 1
  • If one operator is a string and the other operand is a number, convert the string to a number before comparing for equality
  • If one operand is an object and the other operand is not, the valueOf() method of the object is called, and the obtained basic type value is compared according to the previous rules

These two operators follow the following rules when comparing

  • Equal when null and undefined
  • null and undefined cannot be converted to any other value before being compared for equality
  • Returns false for equal operands and true for unequal operands if one of the operands is NaN
  • If both operands are objects, compare whether they are the same object. Equal operands returns true if both operands point to the same object, otherwise, returns false

おすすめ

転載: blog.csdn.net/m0_37149062/article/details/131392788