Logical operators are typically used for Boolean operations, typically used in conjunction with relational operators, with three logical operators: logical AND (and), logical or (or), logical non (not).
1. Logic and (and):&&
var box = (5 > 4) && (4 > 3)//true, true on both sides, returns true
If the operands on either side have an operand that is not a Boolean value, the operation does not necessarily return a Boolean value, at which point the following rule is followed:
1. The first operand is the object, and the second operand is returned;
2. The second operand is an object, the first operand returns true, Returns the second operand, otherwise returns false;
3. One operand is null, and NULL is returned;
4. If one operand is undefined, the undefined is returned.
var box = object && (5 > 4);//true, returns the second operand
var box = (5 > 4) && object;//[object Objects]
var box = (3 > 4) && object;//false
var box = (5 > 4) && null,//null
Logic and operator are short-circuiting operations, GU name If the first operand returns false, the second number returns false, whether true or false.
var box = True && age;//error,//false not defined
var box = False && the age;
2. Logical OR (OR): | |
var box = (9 > 7) | | (7 > 8); True, both sides are true if there is one side, and return true
If the operands on either side have an operand that is not a Boolean value, the logic and operation will not necessarily return a Boolean value, at which point the following rule is followed:
1. The first operand is the object, and the first operand is returned;
2 evaluates to False for the first operand. The second operand is returned,
3. Two operands are objects, the first operand is returned,
4. Two operands are null, NULL is returned,
5. Two operands are Nan, and Nan is returned;
6. Two operands are undefined, The undefined is returned;
var box = Object | | (5 > 3); [Object Object]
var box = (5 > 3) | | object;//true
var box = Object 1 | | object 2;//[object Object]
var bo x = NULL | | Null Null
var box = NaN | | NaN; NaN
var box = undefined | | undefined;//undefined
is similar to the logical AND operator, which is also a short-circuit operation. When the evaluation result of the first operand is true, the second operand is not evaluated.
var box = true |//true
var box = false | | age;//error.
We can take advantage of the logical OR operator feature to avoid assigning null or undefined to a variable Value.
var box = Oneobject | | twoobject;//Assign one of the valid variable values to box
3. Logical non (NOT):!
A logical non-operator can be used with any value. Regardless of the data type of this value, this operator returns a Boolean value. The process is: first convert this value to a Boolean value, and then take the inverse, the rule is as follows:
1. The operand is an object that returns false;
2. The operand is an empty string and returns true;
3. The operand is a non-empty string and returns false;
4. The operand is a value of 0, which returns true;
5. The operand is any non-0 value (including Infinity), false;
6. The operand is null and returns true;
7. The operand is NaN and returns true;
8. The operand is undefined and returns true;
var box =! (5 > 4); False
var box =! {}; False
var box =! "; True
var box =! ' Lee '; False
var box =! 0; True
var box =! 8; False
var box =!null; True
var box =! NaN; True
var box =!undefined; True
Using the logical non-operator once, the process is to turn the value into a Boolean and then reverse. The use of two logical non-operators is to turn the value into a Boolean to reverse the inverse, equivalent to the value of a Boolean () Transformation function processing.
var box =!! 0; False
var box =!! NaN; False
In general, a Boolean value can be obtained using a logical non-operator and two logical non-operators, while there is no error in using more than three logical non-operators, but it does not make sense.
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=gb2312" />5 <title>Operator</title>6 <Scripttype= "Text/javascript">7 /*8 var box = (5 > 4) && (4 > 3);9 alert (box);//true, both sides are true, return TrueTen One A var box = {} && (5 > 4); - alert (box);//true, returns the second operand - the var box = (5 > 4) && {}; - alert (box);//[object Object] - - var box = (3 > 4) && {}; + alert (box);//false - + var box = (5 > 4) && null; A alert (box);//null at - - var box = True && age; - alert (box);//error, age undefined - - var box = False && age; in alert (box);//false, don't perform age. - */ to + - /* the var box = (9 > 7) | | (7 > 8); * alert (box),//true, on both sides as long as true, returns True $ Panax Notoginseng var box = Object | | (5 > 3); - alert (box);//[object Object] the var box = (5 > 3) | | {}; + alert (box);//true A var box = {} | | {}; the alert (box);//[object Object] + var box = null | | null; - alert (box);//null $ var box = NaN | | NaN; $ alert (box);//nan - var box = undefined | | undefined; - alert (box);//undefined the - var box = true | | age;Wuyi alert (box);//true the - var box = false | | age; Wu alert (box);//error, age undefined - var box = Oneobject | | twoobject; About alert (box);//Assign one of the valid variable values to box $ */ - /* - var box =! (5 > 4); - alert (box);//false A + var box =! {}; the alert (box);//false - $ var box =! "; the alert (box);//true the the var box =! ' Lee '; the alert (box);//false - in var box =! 0; the alert (box);//true the About var box =!8; the alert (box);//false the the var box =!null; + alert (box);//true - the var box =! NaN;Bayi alert (box);//true the the var box =!undefined; - alert (box);//true - the the var box =!! 0; the alert (box);//false the - var box =!! NaN; the alert (box);//false the */ the </Script>94 </Head> the the <Body> the Welcome to the world of JavaScript98 </Body> About </HTML>