JavaScript operators--logical operators comprehensive parsing _javascript techniques

Source: Internet
Author: User
Tags logical operators true true

Front.

Logical operators perform Boolean operations on operands and are often used in conjunction with relational operators. Logical operators combine multiple relational expressions to form a more complex expression. Logical operators are divided into logical non '! ', logical and ' && ', logical or ' | | ' 3, this article will introduce the three logical operators

Logical non

Logical non-operator by an exclamation mark (!) Indicates that it can be applied to any value in the ECMAScript. This operator returns a Boolean value regardless of the data type of the value. The logical non-operator first converts its operands to a Boolean value and then the negation

The conversion type of the logical non pair operand to a Boolean type is the same as a Boolean () transformation function, except that it is finally reversed. And if you use two logical non operators at the same time, you actually simulate the behavior of a Boolean () transformation function.

Console.log (!!) undefined);//false
console.log (!!) NULL);//false
console.log (!!) 0);//false
console.log (!!) -0);//false
console.log (!!) NaN);//false
console.log (!! '); /false
console.log (!!) FALSE);//false
Console.log (!!) {});//true
console.log (!!!) []);//true

console.log (!!!) New Boolean (false);//true
console.log (!!) false);//false
console.log (!!) New Boolean (null);//true
console.log (!!) NULL);//false

Logical non-operators are often used to control loops

A Boolean variable (bfound) is used to record whether the retrieval was successful. When the data item in the problem is found, the Bfound will be set to True,!bfound will be equal to false, meaning that the run will jump out of the while loop
var bfound = false;
var i = 0;
while (!bfound) {
 if (avalue[i] = = vsearchvalues) {
  bfound = true;
 } else {
  i++;
 }
}

Logic and

The logic and operators are represented by two and number (&&), two operands, and the result returns true only if two operands are true, or false

Logic and (&&) truth-table number of the first operand the second operand result is true true to True       false       False
false       true        false       alse

Logic and operations can be applied to any type of operand, not just a Boolean value. If one of the operands is not a Boolean, the logic and operation do not necessarily return a Boolean value

Logic and operation are short-circuit operations, if the first operand can determine the result, then the second operand will not be evaluated

For the logic and the case, if the first operand is false, the first operand is returned regardless of the second operand and the result is false, and the second operand is returned if the first operand is true and the second operand is true or false.

In addition to false, undefined, null, +0,-0, NaN, ' These 7 false values, the rest are Truth
console.log (' t ' && ');//Because ' t ' is the truth, so return '
Console.log (' t ' && ' f '); Because ' t ' is the truth value, it returns ' F '
console.log (' t ' && 1 + 2);//Because ' t ' is the truth value, so return 3
console.log (' && ' f ');//Because ' is a false value, so return '
console.log (' && ');//Because ' is a false value, so return '
var i = 1;
var result = (true && i++);
Console.log (result,i);//Because True is the truth value, the execution i++,i is 2,result is 1

var i = 1;
var result = (false && i++);
Console.log (result,i);//Because False is a false value, so i++,i is not performed 1,result is False

Logic and operators can be used multiple, returning the value of an expression with the first Boolean value of False

Console.log (True && ' foo ' && ' && 4 && ' foo ' && true);//'

The precedence ratio of the relational operators is logical with (&&) and logic or (| |) Has a high priority, so similar expressions can be written directly without the addition of parentheses

if (a+1==2 && b+2==3) {
  //todo  
}

You can use logic and operators to replace the IF structure

if (a = = b) {
 dosomething ();
}
Equivalent to
(a = = b) && dosomething ();

Logic and operators are often used in callback functions

If the parameter A is not passed a value, then a is the default undefined, is a false value, so do not execute a (), to prevent errors, if the parameter a is passed the value, then perform function A () functions
fn (a) {
  if (a) {
    a ();
  }
}
//equivalent to
function fn (a) {
  a && a ();
}

Logical OR

Logical OR operator by two vertical bars (| |) Indicates that there are two operands, the result returns false only if two operands are false, otherwise true

Logic or (| |) Truth-table Number of the first operand the second operand result is true true to True True       false
True       true false false       

Similarly, logic or operations can also be applied to any type of operand, not just a Boolean value. If one of the operands is not a Boolean value, the logic or operation does not necessarily return a Boolean value

Logic or operation is also a short-circuit operation, if the first operand can determine the result, then the second operand will not be evaluated

For the logic or the case, returns the first operand if the first operand is true, regardless of the value of the second operand and the result is true; if the first operand is fales, the result is the same as true or false to the second operand, then the second operand

Console.log (' t ' | | //Because ' t ' is the truth value, so return ' t '
console.log (' t ' | | ' F ');//Because ' t ' is the truth value, so return ' t '
console.log (' | | ' F ');//Because ' is a false value, so return ' F '
console.log (' | | //Because ' is a false value, so return '
var i = 1;
var result = (true | | i++);
Console.log (result,i);//Because True is the truth value, so I++,result is not performed true,i is 1

var i = 1;
var result = (False | | i++);
Console.log (result,i);//Because False is a false value, the execution i++,i is 2,result is 1

Similarly, a logical OR operator can be used with multiple, returning the value of an expression with the first Boolean value of True

Console.log (False | | 0 | | '' || 4 | | ' Foo ' | | true);//4

A logical OR operator is often used to set a default value for a variable

If no object is passed in to the parameter p, the default is set to null object
function fn (p) {
  p = p | {};
}

The above is a small series for everyone to bring the JavaScript operator--logical operators to fully parse all the content, I hope that we support cloud-Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.