A total of three Boolean operators: non (not), with (and), or (or)
- 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
- 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
- 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