JavaScript Learning Summary (iv)--a logical OR operator explanation

Source: Internet
Author: User
Tags arithmetic

In JavaScript, the logical OR operator uses the | | Said

|| Bfalse;

The following truth table describes the behavior of the logical AND operator:

Number of Operations 1 Number of Operations 2 Results
True True True
True False True
False True True
False False False

In JavaScript,0, "", False,null,undefined,nan all mean false,

  You can use the following code to prove that:

1   <script type= "Text/javascript" > 2      var bfalse = false;//bool Type 3      var strempty = "";//empty string 4      var IZ Ero = 0;//number is 0 5      var onull=null;//null 6      var oundefined;//undifined 7      var Onan=nan;//nan 8/     * 9     You can also use the logical NOT operator when judging the Boolean value of a JavaScript variable. Doing so requires the use of two not operators in a single line of code.     regardless of the operand type, the first not operator returns a Boolean value. The second not will negate the Boolean value, giving the variable a true boolean value.     */12     document.write ("<PRE>");     Document.writeln ("Boolean false Logical value is" + (!! Bfalse));     Document.writeln ("empty string (\" \ ") logical value is" + (!! Strempty));     Document.writeln ("The logical value of the number 0 is" + (!! Izero));     Document.writeln ("The logical value of Nan is:" + (!! ONaN));     Document.writeln ("Null logical value is" + (!! Onull));     Document.writeln ("Undefined logical value is" + (!! oundefined)     document.write ("</PRE>");   </script>

Operation Result:

  

The logical OR operation of JavaScript is also a simple operation, and for a logical OR operator, if the first arithmetic value is true, the second operand is no longer evaluated , and if an operand is not a Boolean, the logical OR operation does not necessarily return Boolean value , logic | | The operation rules are as follows:

    1. If one operand is an object and the other is a Boolean value, the object is returned.
    2. If two operands are objects, the first object is returned.
    3. Returns null if one of the operands is null.
    4. If an operand is Nan, a nan is returned.
    5. If an operand is undefined, an error occurs.

There is no need to memorize these arithmetic rules, because in JavaScript, you can use the logical NOT operator to determine the Boolean value of a JavaScript variable, and the way to judge it is "!! Variable name ", for example:

use the logical NOT operator to determine the Boolean value of a JavaScript variable

 1 <script type= "Text/javascript" > 2 var bfalse = false;//operand is the number of type bool 3 var sred = "Red";//The operand is a string 4 var Izero = 0;//is 0 5 var ithreefourfive = 345;//operand is any number other than 0 6 var oobject = new Object ();//Object 7 var The onull=null;//operand is null 8 var oundefined;//the operand is undifined 9 var onan=parseint ("abc");//Use the Parseint method to convert the attempt string abc to an integer, as For ABC is not a number, so it is not going to work, so the result of the return is NAN10/*11 to determine the Boolean value of the JavaScript variable, you can also use the logical NOT operator. Doing so requires the use of two not operators in a single line of code. 12 regardless of the type of the operand, the first not operator returns a Boolean value. The second not negates the Boolean value, giving the variable a true boolean value. */14 document.write ("<PRE>"); Document.writeln ("The logical value of Boolean false is" + (!! Bfalse)); Document.writeln ("The logical value of the string sred is" + (!! sred)); Document.writeln ("The logical value of the number 0 is" + (!! Izero)); Document.writeln ("The logical value of the number 345 is" + (!! ithreefourfive)); Document.writeln ("The logical value of object is" + (!! Oobject)); Document.writeln ("The logical value of Nan is:" + (!! ONaN)); Document.writeln ("Null logical value is" + (!! Onull)); documEnt.writeln ("Undefined logical value is" + (!! oundefined)); document.write ("</PRE>"); </script>

Judging Result:

  

Logic | | Operator Test Script:

 1 <script type= "Text/javascript" > 2 document.write ("<PRE>"); 3/*javascript | | Returns the first value that is not false (also the object) or the last value (if all is false) */4 var a=2; 5 var b=90; 6 var c=a| | b;//in JavaScript, a number other than 0 represents true,0 as false 7 Document.writeln ("a=2,b=90,c=a| |      The result of B is: "+c);//The result is 2, which returns the first value that is not false, so the value is 2 8 9 var bfalse1=false;10 var bfalse2=false;11 var num=0;//0 represents false12 var result=bfalse1| | bfalse2| | Num;13 Document.writeln ("bfalse1=false,bfalse2=false,num=0,result=bfalse1| | bfalse2| | The result of Num is: "+num);//If all are false, the last value is returned, so the result is 014 15/* If one operand is an object and the other is a Boolean value, the object is returned. */16 var obj = new Object (); var btrue=true;18 var bfalse=false;19 document.writeln ("obj| | The result of Btrue is: "+ (obj| | btrue));//Return object20 Document.writeln ("btrue| | The result of obj is: "+ (btrue| | obj));//Returns True, a logical OR operation is also a simple operation. For a logical OR operator, if the first operand is true, the second operand is no longer evaluated. Document.writeln ("obj| | The result of Bfalse is: "+ (obj| | Bfalse));//Return object22 Document.writeln ("bfalse| | The result of obj is: "+ (bfalse| | obj));//Return OBJECT23 24/* If two operands are objects, return the first object */25 var obj1 = new Object (); var obj2 = new Object (); Document.writeln ("obj1== (obj1| | OBJ2) The result is: "+ (obj1== (obj1| | OBJ2));//Results for True28 Document.writeln ("obj2== (obj1| | OBJ2) The result is: "+ (obj2== (obj1| | OBJ2)));//The result is false29 30/* If an operand is null, returns NULL. */31 var c=1;32 var d=null;33 document.writeln ("D=null,null boolean value is;" + (!! d)); Document.writeln ("c=1,d=null,c| | The result of D is: "+ (c| | d)); Document.writeln ("c=1,d=null,d| | The result of C is: "+ (d| | c); o=new var (); Document.writeln ("O is an object, d=null,o| | The result of D is: "+ (o| | d));//returns object39 Document.writeln ("O is an object, d=null,d| | The result of O is: "+ (d| | o));//Return OBJECT40-var zero=0;42 document.writeln ("zero=0,d=null,zero| | The result of D is: "+ (zero| | d));//Return null43 Document.writeln ("zero=0,d=null,d| | The result of zero is: "+ (d| | zero));//return 044 var btrue = true;46 var bresult = (Btrue | | bunknown); 47/* Variable bunknown is undefined. However, because the value of the variable btrue is True,bunknown is not evaluated, the output is "true". */48 Document.writeln ("Bunknown is undefined as a variable, bresult = (Btrue | |    Bunknown) The result is: "+bresult);//Output" true "Bfalse = false;51 bresult = (Bfalse | | bunknown);            An error occurred in Document.writeln (Bresult); Does not perform this line of document.write ("</PRE>");

Operation Result:

JavaScript Learning Summary (iv)--a logical OR operator explanation

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.