JavaScript Knowledge Point Summary (iv) Logical OR operator detailed _javascript tips

Source: Internet
Author: User
Tags arithmetic

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

var btrue = true;
var bfalse = false;

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

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

You can use the following code to prove:

<script type= "Text/javascript" >
var bfalse = false;//bool type
var strempty = "";//empty string
var Izero =;//number is var
onull=null;//null var
oundefined;//undifined
var onan=nan;//nan/
*
to determine JavaScript variables Boolean value, you can also use the logical NOT operator. Doing so requires the use of two not operators in one line of code.
the first not operator returns a Boolean value, regardless of the type of OP count. The second not negates the Boolean value, giving the variable a true boolean value.
* *
document.write ("<PRE>");
Document.writeln (The logical value of the Boolean false is "+" (!!). Bfalse));
The logical value of the Document.writeln ("\") is "+ (!!! Strempty));
Document.writeln ("The logical value of the number is" + (!!) Izero));
Document.writeln ("Nan's logical value is:" + (!!) ONaN));
Document.writeln ("Null logical value is" + (!!) Onull));
Document.writeln ("Undefined logical value is" + (!!) oundefined));
document.write ("</PRE>");

Run Result:

 

The logical or operation of JavaScript is also a simple operation, and for the logical OR operator, if the first operation value is true, the second operand is no longer computed, and if one of the operands is not a Boolean, the logical OR operation does not necessarily return a Boolean value , Logic | | The operation rules are as follows:

1. If one operand is an object and the other is a Boolean value, return the object.

2. If the two operands are objects, the first object is returned.

3. If a shipment count is NULL, return NULL.

4. If a shipping count is Nan, return nan.

5. If an op count is undefined, an error occurs.

For these rules of operation, there is no need to cram, because in JavaScript, you can use the logical NOT operator to determine the Boolean value of JavaScript variables, the way to judge is "!! Variable name ", for example:

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

 <script type= "Text/javascript" > var bfalse = false;//op arithmetic is the number of bool type var sred = "Red"; /Op arithmetic is a string var Izero =;//op arithmetic is var ithreefourfive =//OP arithmetic is any number other than the numeric var oobject = new Object ();/object var onull=null;//op is nul L var oundefined;//is undifined var onan=parseint ("abc");//Use the Parseint method to convert the attempt string abc to an integer, because ABC is not a number and therefore cannot be turned. So the result of the return is Nan/* The Boolean value of a JavaScript variable can be judged by using the logical NOT operator.
Doing so requires the use of two not operators in one line of code. The first not operator returns a Boolean value, regardless of the type of OP count.
The second not negates the Boolean value, giving the true Boolean value of the variable.
* * document.write ("<PRE>"); Document.writeln (The logical value of the Boolean false is "+" (!!).
Bfalse)); Document.writeln ("The logical value of the string sred is" + (!!)
sred)); Document.writeln ("The logical value of the number is" + (!!)
Izero)); Document.writeln ("The logical value of the number is" + (!!)
ithreefourfive)); Document.writeln ("The logical value of Object objects is" + (!!)
Oobject)); Document.writeln ("Nan's logical value is:" + (!!)
ONaN)); Document.writeln ("Null logical value is" + (!!)
Onull)); Document.writeln ("Undefined logical value is" + (!!)
oundefined));
document.write ("</PRE>"); </script> 

Judgment Result:

Logic | | Operator Test Script:

<script type= "Text/javascript" > document.write ("<PRE>"); /*javascript in the | |
Returns the first value that is not false (the object also) or the last value (if all false)/Var a=;
var b=; var c=a| | b;//in JavaScript, a non-numeric number represents true, representing false Document.writeln ("a=,b=,c=a| |
The result of B is: "+c";//The result is, returns the first value that is not false, so the value is Var bfalse=false;
var Bfalse=false; var num=;//represents false var result=bfalse| | bfalse| |
Num Document.writeln ("bfalse=false,bfalse=false,num=,result=bfalse| | bfalse| | The result of Num is: "+num";//If All is false, returns the last value, so the result is/* if one of the operands is an object and the other is a Boolean value, return the object.
* * var obj = new Object ();
var btrue=true;
var Bfalse=false; Document.writeln ("obj| | The result of the Btrue is: "+ (obj| | btrue))//Returns Object Document.writeln ("btrue| | The result of obj is: "+ (btrue| | obj));//returns True, and the logical OR operation is also a simple operation.
For the logical OR operator, if the first operation value is true, the second operand is no longer evaluated. Document.writeln ("obj| | The result of the Bfalse is: "+ (obj| | Bfalse))//Returns Object Document.writeln ("bfalse| | The result of obj is: "+ (bfalse| |
obj);//Return object/* If two operands are objects, return the first object/var obj = new Object ();
var obj = new Object (); Document.writeln ("obj==" (obj| | obj), the result is: "+ (obj== obj| | obj));//The result isTrue Document.writeln ("obj==" (obj| | obj), the result is: "+ (obj== obj| | obj));//The result is false/* returns NULL if one of the operands is null.
* * var c=;
var d=null; Document.writeln ("The Boolean value of the d=null,null is;" + (!!
d)); Document.writeln ("c=,d=null,c| | The result of D is: "+ (c| |
d)); Document.writeln ("c=,d=null,d| | The result of C is: "+ (d| |
c));
var o=new Object (); Document.writeln ("O is an object, d=null,o| | The result of D is: "+ (o| | d)//Return object Document.writeln ("O is an object, d=null,d| | The result of O is: "+ (d| |
o);/return object Var zero=; Document.writeln ("zero=,d=null,zero| | The result of D is: "+ (zero| | d);/return null Document.writeln ("zero=,d=null,d| | The result of zero is: "+ (d| |
zero);//returns var btrue = true;
var bresult = (Btrue | | bunknown); /* variable bunknown is undefined. However, the output is "true" because the value of the variable btrue is true,bunknown and is not evaluated. */Document.writeln ("Bunknown is an undefined variable, bresult = (Btrue | |
Bunknown), the result is: "+bresult";/output "true" Bfalse = false; Bresult = (Bfalse | | bunknown); An error occurred document.writeln (bresult);  This line of document.write ("</PRE>") will not be executed;

Run Result:

The above content is small series to introduce the JavaScript Knowledge Point Summary (iv) of the logic or operator detailed, hope to help everyone!

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.