JavaScript operators--logical operators

Source: Internet
Author: User
Tags true true

xCatalog [1] Logic not [2] logic with [3] logic or previous words

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 '! ', logic and ' && ', logic or ' | | ' 3, this article will describe these three kinds of logical operators

Logical Non-

The logical non-operator consists of an exclamation mark (!) Represents any value that can be applied to the ECMAScript. Regardless of the data type of this value, this operator returns a Boolean value. The logical non-operator first converts its operand to a Boolean value, and then the negation

The conversion type of a logical non-pair operand to a Boolean type is the same as a Boolean () transformation function, except that the result is finally reversed. If you use two logical non-operators at the same time, you will 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 (!! {}); // trueconsole.log (!! []); // true console.log (!! New Boolean (false)); // trueconsole.log (!! false); // falseConsole.log (!! New Boolean (null)); // trueconsole.log (!! null); // false

Logical non-operators are often used to control loops

// The 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 while the loop varisfalse; var i = 0;  while (! bfound) {  if (avalue[i] = = vsearchvalues)    {true;   Else {    i+ +;  }}

Logic and

The logical AND operator are represented by two and numbers (&&), with two operands, and the result returns true only if two operands are true, otherwise false

// logic and the Truth Table of (&&) first operand        second operand        result                               true              true True true false to False                           true                false              false              alse

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

Logic and operation are short-circuiting operations, and if the first operand determines the result, the second operand is no longer evaluated

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

// In addition to false, undefined, null, +0,-0, NaN, ' ' 7 false values, the rest are truth // because ' t ' is the truth, so return ' // because ' t ' is the true value, so return ' F ' // because ' t ' is the true value, so return 3 // because ' is a false value, so return ' // because ' is a false value, so return '
var i = 1; var result = (true && i++); Console.log (result,i); // because True is the truth, so 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, do not execute I++,i is 1,result is false

The logical AND operator can be used with multiple, returning the value of the first expression with a Boolean value of False

Console.log (truetrue); // "'

Relational operators have precedence over logic (&&) and logic or (| |) High priority, so similar expressions can be written directly without the need to add parentheses

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

You can use the logical AND operator 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 the value, then a is the default undefined, is a false value, so do not execute a (), to prevent error, if the parameter a value, then execute function a () function fn (a) {    if(a) {        a ();}    } // equivalent to function fn (a) {    && A ();}

Logical OR

A logical OR operator consists of two vertical bars (| |) Indicates that there are two operands, and only if the two operands are false, the result returns false, otherwise true

// logic or (| |) Truth table The first operand the        second operand        results truetrue True                                           false             true false             true              true false             false             false

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

The logic or operation is also a short-circuit operation, and if the first operand can determine the result, then the second operand is no longer evaluated

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

Console.log (' t ' | | ‘‘); // because ' t ' is true, it returns "T"console.log (' t ' | | ' F '); // because ' t ' is the true value, return ' t' Console.log (' | | ' F '); // because ' is a false value, 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 do not execute I++,result is 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 more than once, returning the value of the first expression with a Boolean value of True

Console.log (falsetrue); // 4

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

// if no object is passed to the parameter p, the parameter is set to an empty object by default function fn (p) {    = P | |  {};}

Resources

"1" es5/expression https://www.w3.org/html/ig/zh/wiki/ES5/expressions
"2" Ruan one peak JavaScript standard reference Tutorial--syntax--Boolean operator http://javascript.ruanyifeng.com/
"3" W3school-javascript advanced tutorial--ecmascript logical operator http://www.w3school.com.cn/
"4" JavaScript authoritative Guide (6th edition), chapter 4th expressions and operators
"5" JavaScript Advanced Programming (3rd Edition), chapter 3rd Basic Concepts
"6" JavaScript DOM Programming Art (2nd Edition), chapter 2nd JavaScript syntax

JavaScript operators--logical operators

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.