logical operators for JavaScript

Source: Internet
Author: User
Tags object object

Logical operators are typically used for Boolean operations, typically used in conjunction with relational operators, with three logical operators: logical AND (and), logical or (or), logical non (not).

First, logic and (and):&&

var // true, both sides are true, return True


If the operands on both sides have an operand that is not a Boolean value, the operation does not necessarily return a Boolean value, at which point the following rule is followed:
1. The first operand is the object, then the second operand is returned;
2. The second operand is the object, and the first operand returns true to return the second operand, otherwise false;
3. If one of the operands is NULL, returns null;
4. If one of the operands is undefined, the undefined is returned.

    varbox = {} && (5 > 4);//true to return the second operand    varbox = (5 > 4) && {};//[Object Object]    varbox = (3 > 4) && object;//false    varbox = (5 > 4) &&NULL;//NULL    varbox =NULL&& (5 > 4); varbox = undefined && (5 > 4);//undefined    varbox = (5 > 4) && undefined;//Ibid .

The logical AND operator are short-circuiting operations, as the name implies, if the first operand returns false, the second number, whether true or false, will no longer return the operation directly to false.

var true // error, age not defined var false // false, do not perform age.



Second, logical OR (OR): | |

var // true, on both sides as long as there is true on one side, returns True


If the operands on both sides have an operand that is not a Boolean value, the logic and operation do not necessarily return a Boolean value, at which point the following rule is followed:
1. The first operand is the object, then the first operand is returned;
2. The first operand evaluates to False, then returns the second operand;
3. Two operands are objects, the first operand is returned;
4. Two operands are null, then NULL is returned;
5. The two operands are Nan, then the Nan is returned;
6. Two operands are undefined, then return undefined;

 var  box = {} | | (5 > 3); // [object Object]  var  box = (5 > 3) | | {}; // true  var  box = {} | | {} ; // [object Object]  var  box = null  | | null ; // null  var  box = NaN | | NaN; // nan  var  box = undefined | | Undefined // undefined  

Like logic and operators, a logic or operator is also a short-circuit operation. When the evaluation result of the first operand is true, the second operand is not evaluated.

var true // true var false // error, age not defined

We can use the logical OR operator feature to avoid assigning null or undefined values to variables.

var // assign one of the valid variable values to box


Third, logical non (not):!
A logical non-operator can be used with any value. Regardless of the data type of this value, this operator returns a Boolean value. The process is: first convert this value to a Boolean value, and then take the inverse, the rule is as follows:
1. The operand is an object that returns false;
2. The operand is an empty string and returns true;
3. The operand is a non-empty string and returns false;
4. The operand is a value of 0, which returns true;
5. The operand is any non-0 value (including Infinity), false;
6. The operand is null and returns true;
7. The operand is NaN and returns true;
8. The operand is undefined and returns true;

varbox =! (5 > 4);//falsevarbox =! {};//falsevarbox =! ';//truevarbox =! ' Lee ';//falsevarbox =! 0;//truevarbox =! 8;//falsevarbox =!NULL;//truevarbox =! NaN;//truevarbox =!undefined;//true

Using the logical non-operator once, the process is to turn the value into a Boolean and then reverse. The use of two logical non-operators is to turn the value into a Boolean to reverse the inverse, equivalent to the value of a Boolean () Transformation function processing.

var // false var // false

logical operators for JavaScript

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.