Js logical AND or operator

       The logical AND operation can be applied to any type of operand, not just booleans. In the case where one of the operands is not a boolean, the logical AND operation does not necessarily return a boolean; in this case, it follows the following rules:

 If the first operand is an object, return the second operand;

 If the second operand is an object, the object will only be returned if the first operand evaluates to true;

 If both operands are objects, return the second operand;

 If one of the operands is null, return null;

 If one of the operands is NaN, return NaN;

 Returns undefined if one of the operands is undefined.

      The logical AND operation is a short-circuit operation, that is, if the first operand can determine the result, then the second operand will not be evaluated. For logical AND operations, if the first operand is false, the result can no longer be true, regardless of the value of the second operand. Consider the following example:

 

var found = true;
var result = (found && someUndefinedVariable); // an error occurs here
alert(result); // this line will not execute

      In the above code, an error occurs when the logical AND operation is performed because the variable someUndefinedVariable is not declared. Since the value of the variable found is true, the AND operator continues to evaluate the variable someUndefinedVariable. But someUndefinedVariable is not yet defined, so it causes the error. This means that undefined values ​​cannot be used in logical AND operations. If the value of found is set to false, as in the following example, the error will not occur:

var found = false;
var result = (found && someUndefinedVariable); // no error occurs
alert(result); // will execute ("false")

       In this example, an alert box will be displayed. Whether the variable someUndefinedVariable is defined or not, it will never be evaluated because the value of the first operand is false. This also means that the result of the logical AND operation must be false, and there is no need to evaluate the operand on the right side of the && at all. When using the logical AND operator always keep in mind that it is a short-circuiting operator.

 

         Similar to the logical AND operation, the logical OR does not necessarily return a Boolean value if one of its operands is not a Boolean value; in this case, it follows the following rules:

 If the first operand is an object, return the first operand;

 If the first operand evaluates to false, return the second operand;

 If both operands are objects, return the first operand;

 If both operands are null, return null;

 If both operands are NaN, return NaN;

 Returns undefined if both operands are undefined.

 

Default first operand (transformed) is true, return the first, otherwise return the second

 

Like the logical AND operator, the logical OR operator is also a short-circuiting operator. That is, if the first operand evaluates to true, the second operand will not be evaluated. Let's see an example:

var found = true;
var result = (found || someUndefinedVariable); // no error occurs
alert(result); // will execute ("true")

  We can take advantage of this behavior of logical OR to avoid assigning null or undefined values ​​to variables. E.g:

var myObject = preferredObject || backupObject;

 In this example, the variable myObject will be assigned one of the two values ​​following the equals sign. The variable preferredObject contains the value assigned to the variable myObject first, and the variable backupObject is responsible for providing a backup value in the event that preferredObject does not contain a valid value. If the value of preferredObject is not null, then its value will be assigned to myObject; if it is null, the value of backupObject will be assigned to myObject. Assignment statements in ECMAScript programs often use this pattern, and this pattern will be used throughout this book.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326159602&siteId=291194637