JavaScript basic learning series twenty-one: logical or

The logical OR operator is represented by two pipe characters (||), such as:

let result = true || false;

The logical OR operator follows the following truth table:

Insert image description here
If one of the operands is not a Boolean value, the logical OR operator does not necessarily return a Boolean value. It follows the following rules.

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

Similar to logical AND, logical OR operator also has short-circuiting properties. It's just that for logical OR, if the first operand evaluates to true, the second operand will not be evaluated again.

let found = true;
let result = (found || someUndeclaredVariable); // 不会出错 console.log(result); // 会执行

Like the previous example, the variable someUndeclaredVariable is not defined. However, because the value of variable found is true, the logical OR operator does not evaluate the variable someUndeclaredVariable and returns true directly. If the value of found is changed to false.

let found = false;
let result = (found || someUndeclaredVariable); // 这里会出错 console.log(result); // 不会执行这一行

The variable myObject will be assigned one of two values. Among them, the preferredObject variable contains the preferred value, and the backupObject variable contains the alternate value. If preferredObject is not null, its value will be assigned to myObject; if preferredObject is null, the value of backupObject will be assigned to myObject. this

This pattern is often used for variable assignment in ECMAScript code. ,

1. Multiplicative operator:

ECMAScript defines three multiplicative operators: multiplication, division and modulo. These operators work the same as their counterparts in Java, C, and Perl, but they also include some automatic type conversion when working with non-numeric values.

If a multiplicative operator has an operand that is not a numeric value, the operand will be converted to a numeric value using the Number() conversion function behind the scenes. This means that an empty string is treated as 0, and a Boolean value of true is treated as 1.

The multiplication operator also has some special behavior when dealing with special values.

  • If the operands are both numeric values, the normal multiplication operation is performed, that is, the multiplication of two positive values ​​is a positive value, the multiplication of two negative values ​​is also a positive value, and the multiplication of values ​​with different positive and negative signs results in a negative value. If ECMAScript cannot represent the product, Infinity or -Infinity is returned.
  • If either operand is NaN, NaN is returned.
  • If Infinity times 0, NaN is returned.
  • If Infinity is multiplied by a non-zero finite value, Infinity or -Infinity is returned depending on the sign of the second operand.
  • If Infinity times Infinity, Infinity is returned.
  • If there are operands that are not numeric values, they are first converted to numeric values ​​using Number() in the background, and then the above rules are applied.

Guess you like

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