The logical NOT operator is represented by an exclamation point !
and may be applied to any value in Javascript.
This operator always returns a Boolean value, regardless of the data type it's used on.
The logical NOT operator first converts the operand to a Boolean value and then negates it. The logical NOT behaves in the following ways:
console.log(!false); //true
console.log(!"asdf"); //false
console.log(!0); //true
console.log(!NaN); //true
console.log(!""); //true
console.log(!12); //false
The code above generates the following result.
The logical NOT operator can be used to convert a value into its Boolean equivalent.
By using two NOT operators, you can simulate the Boolean() casting function.
console.log(!!"asdf"); //true
console.log(!!0); //false
console.log(!!NaN); //false
console.log(!!""); //false
console.log(!!12); //true
The code above generates the following result.
The boolean operator AND operator is represented by the double ampersand <<
and is applied to two values:
var result = true && false;
Boolean operator AND behaves as described in the following truth table:
OPERAND 1 | OPERAND 2 | RESULT |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Boolean operator AND can be used with any type of operand, not just Boolean values.
When either operand is not a primitive Boolean, AND does not always return a Boolean value; instead, it does one of the following:
AND is a short-circuited operator: if the first operand determines the result, the second operand is not evaluated.
var found = true; var result = (found && someUndeclaredVariable); //error occurs here console.log(result); //this line never executes found = false; var result = (found && someUndeclaredVariable); //no error console.log(result); //works
The Boolean Operator OR is represented by the double
pipe ||
in Javascript, like this:
var result = true || false;
OR behaves as described in the following truth table:
OPERAND 1 | OPERAND 2 | RESULT |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
If either operand is not a Boolean, logical OR will not always return a Boolean value; instead, it does one of the following:
OR operator is short-circuited. In this case, if the first operand evaluates to true, the second operand is not evaluated.
var found = true; var result = (found || someUndeclaredVariable); //no error console.log(result); //works found = false; var result = (found || someUndeclaredVariable); //error occurs here console.log(result); //this line never executes
Because the variable found
is set to true, the variable someUndefinedVariable
is never evaluated
and thus the output is "true". If the value of found is changed to false, an error will occur.
To avoid assigning a null or undefined value to a variable, use the following code.
var myObject = preferredObject || backupObject;
If preferredObject isn't null, then it's assigned to myObject; if it is null, then backupObject is assigned to myObject.