In JavaScript, logical OR operators are represented by ||
var bTrue = true;var bFalse = false;var bResult = bTrue || bFalse;
The following truth table describes the behavior of logical AND operators:
In JavaScript , 0, "", false, null, undefined, and NaN all means false .
It can be proved with the following code:
<script type="text/javascript">var bFalse = false;//bool type var strEmpty = "";//empty string var iZero = ;//null is var oNull=null;//nullvar oUndefined;//undifinedvar oNaN=NaN;//NaN/* When judging the Boolean value of a JavaScript variable, you can also use the logical NOT operator. Doing so requires the use of two NOT operators in a line of code. Regardless of the type of the operand, the first NOT operator returns the Boolean value. The second NOT will negatively count the Boolean value, giving the true Boolean value of the variable. */document.write("<PRE>");document.writeln("The logical value of boolean false is " + (!!bFalse));document.writeln("The logical value of empty string(/"/") is " + (!!strEmpty));document.writeln("The logical value of number is " + (!!iZero));document.writeln("NaN's logical value is " + (!!oNaN));document.writeln("null's logical value is " + (!!oNaN));document.writeln("undefined's logical value is " + (!!oUndefined));document.write("</PRE>");</script>Running results:
JavaScript's logical OR operation is also a simple operation. For logical OR operators, if the first operation value is true, the second operation will no longer be calculated. If a certain operation is not a Boolean value, the logical OR operation does not necessarily return a Boolean value . The operation rules of logical || are as follows:
1. If one operation is an object and the other is a Boolean value, return the object.
2. If both operations are objects, return the first object.
3. If a certain operation number is null, return null.
4. If a certain operation number is NaN, return NaN.
5. If a certain operation number is undefined, an error occurs.
There is no need to memorize these operation rules, because in JavaScript, you can use logical Not operators to judge the Boolean value of JavaScript variables, and the way to judge is "!! variable name", for example:
Use logical Not operators to determine the Boolean value of JavaScript variables
<script type="text/javascript">var bFalse = false;//The operand is a number of bool type var sRed = "red";//The operand is a string var iZero = ;//The operand is var iThreeFourFive = ;//The operand is any number other than var oObject = new Object();//Object var oNull=null;//The operand is nullvar oUndefined;//The operand is undifinedvar oNaN=parseInt("abc");//Use parseInt method to convert the attempted string abc into an integer. Because abc is not a number, it cannot be converted. Therefore, the returned result is that when NaN/* judges the Boolean value of a JavaScript variable, you can also use the logical NOT operator. Doing so requires the use of two NOT operators in a line of code. Regardless of the type of the operand, the first NOT operator returns the Boolean value. The second NOT will inverse the Boolean value, giving the true Boolean value of the variable. */document.write("<PRE>");document.writeln("The logical value of boolean false is " + (!!bFalse));document.writeln("The logical value of string sRed is " + (!!sRed));document.writeln("The logical value of number is " + (!!iZero));document.writeln("The logical value of number is " + (!!iThreeFourFive));document.writeln("The logical value of object is " + (!!oObject));document.writeln("NaN's logical value is " + (!!oNaN));document.writeln("null's logical value is " + (!!oNull));document.writeln("undefined's logical value is " + (!!oUndefined));document.write("</PRE>");</script>Judgment result:
Logical|| operator test script:
<script type="text/javascript">document.write("<PRE>");/*| in JavaScript will return the first value that is not false (objects are also possible) or the last value (if all are false)*/var a=;var b=;var c=a||b;//In JavaScript, non-numbers represent true, which means falsedocument.writeln("a=,b=,c=a||b The result of: "+c);//The result is, the first value that is not false is returned, so the value is var bFalse=false;var bFalse=false;var num=;//Represents falseavar result=bFalse||bFalse||num;document.writeln("bFalse=false,bFalse=false,num=,result=bFalse||bFalse||num The result of result is: "+num);//If all are false, the last value is returned, so the result is /*If one operation is an object and the other is a Boolean value, the object is returned. */var obj = new Object();var bTrue=true;var bFalse=false;document.writeln("obj||bTrue result is: "+(obj||bTrue));//Return objectdocument.writeln("bTrue||obj result is: "+(bTrue||obj));//Return true, logical OR operation is also a simple operation. For logical OR operators, if the first operation value is true, the second operation will no longer be calculated. The result of document.writeln("obj||bFalse is: "+(obj||bFalse));//Return objectdocument.writeln("bFalse||obj is: "+(bFalse||obj));//Return object/*If both operations are objects, return the first object*/var obj = new Object();var obj = new The result of Object();document.writeln("obj==(obj||obj) is: "+(obj==(obj||obj)));//The result is truedocument.writeln("obj==(obj||obj)));//The result is false/*If a certain operation is null, return null. */var c=;var d=null;document.writeln("d=null,null boolean value is; "+(!!d));document.writeln("c=,d=null,c||d result is: "+(c||d));document.writeln("c=,d=null,d||c result is: "+(d||c));var o=new Object();document.writeln("o is an object, d=null,o||d result is: "+(o||d));//Return objectdocument.writeln("o is an object, d=null,d||o is: "+(d||o));//Return objectvar zero=;document.writeln("zero=, d=null,zero||d result is: "+(zero||d));//Return nulldocument.writeln("zero=, d=null,d||zero result is: "+(d||zero));//Return var bTrue = true;var bResult = (bTrue || bUnknown);/*The variable bUnknown is undefined. However, since the value of the variable bTrue is true, bUnknown will not be calculated, so the output is "true". */document.writeln("bUnknown is an undefined variable. The result of bResult = (bTrue || bUnknown) is: "+bResult);//Output "true"bFalse = false;bResult = (bFalse || bUnknown); //Error document.writeln(bResult); //This line will not be executed document.write("</PRE>");Running results:
The above content is a detailed explanation of the logical OR operator of the JavaScript knowledge points summarized (IV) introduced by the editor. I hope it will be helpful to everyone!