JS Basic Concept--operator--Boolean operator

Source: Internet
Author: User

A total of three Boolean operators: non (not), with (and), or (or)

  1. Logical Non-a logical non-operator is represented by an exclamation mark (!) that can be applied to any value in the ECMAScript. Regardless of the type of this value, this operator returns a Boolean value. The logical non-operator first converts his operand to a Boolean value, Againto reverse it.
    //1. If the operand is an object, return Falsevaro ={name:"SS"};alert (!o);//falsevarO1 ={};alert (!O1);//false//2. If the operand is an empty string, returns TruevarSTR1 = ""; alert (!STR1);//true//3. Returns False if the operand is a non-empty stringvarstr2 = "Name"; alert (!STR2);//false//4. If the operation value is 0, return truevarnum = 0; alert (!num);//true//5. Returns False if the operand is any non-0 value (including infinity)varNUM1 = 23; alert (!NUM1);//falsevarnum2 = Number.MAX_VALUE *Number.max_value;alert (!NUM2);//false//6. Returns true if the operand is nullvarSTR3 =NULL; alert (!STR3);//true//7. Returns true if the operand is NanvarSTR4 = "CCC"; STR4++; alert (STR4); //NaNalert (!STR4);//true//8. If the operand is undefined, return truevarVal;alert (Val); //undefinedalert (!val);//true

    A logical non-operator can also be used to convert a value to its corresponding Boolean value. Using two logical non-operators, in effect, simulates the behavior of a Boolean () transformation function. The end result is the same as using the Boolean () function for this value.

    // truealert (!! 0);        // falsealert (!! NaN);    // falsealert (!! ");    // false
  2. Logic and

    Logic and operator by two and number (&&)


    Logic is a short-circuit operation where the second operand is no longer evaluated if the first operand can determine the result. For logic and operations, if the first operand is false, no matter what the second operand is, the result is not likely to be true.

     var  found = true      var  result = (found && someundefinedvariable); //     alert (result) will occur here; //  This line does not execute  //  var  found1 = false  ; var  result1 = (found1 &&    someundefinedvariable);    alert (RESULT1);  // false   
  3. Logical OR

    A logical OR operator consists of two vertical bar symbols (| | ) indicates that there are two operands.

    A logical OR operator is also a short-circuit operation, and if the first operand evaluates to true, the second operand is not evaluated.

     var  found = false      var  result = (Found | | someundefinedvariable); //     alert (result) will occur here; //  This line does not execute  //  var  found1 = true  ; var  result1 = (Found1 | |     someundefinedvariable);    alert (RESULT1);  // true   

    We can use the logic or this behavior to avoid assigning null or undefined values to variables.

    var Preferrdobject; var backupobject = "CCC"var myObject = Preferrdobject | | Backupobject;alert (myObject);     // CCC /* */

JS Basic Concept--operator--Boolean operator

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.