Javascript-boolean operator

Source: Internet
Author: User

There are three types of boolean operators:

1. Logic not

Logic not defined! Different from the logical or and logical and operators: the logical not operator must return a Boolean value.

The action of the logical not operator is as follows:

      • Returns false if the number of operations is an object.
      • If the number of operations is any number other than 0, false is returned.
      • Returns true if the number of operations is 0.
      • Returns true if the number of operations is null.
      • Returns true if the number of operations is Nan.
      • An error occurs if the number of operations is undefined.
      • Evaluate the Boolean value to obtain the true Boolean value.

Example:

Function display (Value) {Document. Write (Value+"<Br/>");} Display (!NewObject (); display (! 100); display (! 0); display (!Null); Display (! NAN); display (!True); Display (!False);// UndefinedDisplay (! SS );

Result:

Summary:

  • If the number of operations is 0, null, Nan, and undefined, the result is true if the value is not specified.
  • If the number of operations is any number or object other than 0, the result is false.

2. Logical and operator

The logical and operator is represented. logic and is a simple operation, that is, if the first operation determines the result, the second operation is no longer calculated. For logic and, if the first operation is false, no matter what the value of the second operation is, it cannot be true.

The following describes the true values:

First operand Second operand Result
True True True
True False False
False True False
False False False

The Operation Number of the logical and operator can be of any type, not only a Boolean value. If a certain operation number is not the original Boolean value, the logical and operator does not necessarily return a Boolean value.

      • If the first operation is an object, the second operation is returned.
      • If the number of operations is null, null is returned.
      • If the number of operations is Nan, Nan is returned.
      • An error occurs if the number of operations is undefined.

Example:

Function display ( Value ) {Document. Write ( Value +" <Br/> ");} Var truevalue = True ; Var falsevalue = False ; Var obj1 = New Object (); obj1.name =" Obj1 "; Var obj2 = New Object (); obj2.name =" Obj2 "; Var result; // If the first operation is an object and the other operation is a Boolean value, the return value is boolean. Result = obj1 & truevalue; display (result); Result = obj1 & falsevalue; display (result ); // If the first operation is a Boolean value, the operation is executed in a simple operation mode. Result = truevalue & obj1; // Obj1 & true; Display (result. Name); Result = falsevalue & obj1; display (result); display (truevalue & 'www ') // If both operations are objects, the second object is returned. Result = obj1 & obj2; display (result. Name );// If the number of operations is null, null is returned. Result = obj1 && Null ; Display (result ); // If the number of operations is Nan, Nan is returned. Result = obj1 & Nan; display (result ); // An error occurs if the number of operations is undefined. Result = obj1 & SS; display (result );

Result:

Summary:

  • When the variable is null, Nan, or undefined, it is the same as false and has the veto power. null, Nan, and undefined are returned respectively.
  • When the number of operations is an object, all operations are performed. If the first operation is performed, the second operation is returned.
  • An error is thrown when the number of operations is not declared.

3. Logical or operator

the logical or operator is represented by |. The logical or operator is also a simple operation. If the first operation is true, the second operation is not calculated.

The following describes the true values:

first operand second operand result
true true true
true false true
false true true
false false false

The number of operations of the logical or operator can be of any type, not just a Boolean value. If the number of operations is not the original Boolean value, the logical or operator does not necessarily return a Boolean value.

      • If the first operation is an object, this object is returned.
      • If the first operation is false, null, Nan, undefined, and the second operation is null, null is returned.
      • If the first operation is false, null, Nan, undefined, and the second operation is Nan, Nan is returned.
      • If the first operation is false, null, Nan, undefined, and the second operation is undefined, an error is thrown.

Example:

Function display ( Value ) {Document. Write ( Value +" <Br/> ");} Var truevalue = True ; Var falsevalue = False ; Var obj1 =New Object (); obj1.name =" Obj1 "; Var obj2 = New Object (); obj2.name =" Obj2 "; Var result; // If the first operation number is an object, this object is returned. Result = obj1 | truevalue; display (result. name); Result = obj1 | falsevalue; display (result. name); Result = obj1 | obj2; display (result. name ); // If the first operation is false, null, Nan, undefined, and the second operation is null, null is returned. Display ( False | Null ); // If the first operation is false, null, Nan, undefined, and the second operation is Nan, Nan is returned. Display ( Null | Nan ); // If the first operation is false, null, Nan, undefined, and the second operation is undefined, an error is thrown. Display (Nan | SS); display (SS | SS );

Result:

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.