AND (&&), Not (!), OR (||) are logical operators in Javascript. You will see that they are the only operators that make up for the entirety of one’s life experiences.
Logical operators evaluate two values or expressions, and provide a true or false result.
Let us see some examples to know the rules of the game.
AND
Only trues get along with each other to provide a true result - when dealing with an AND operation.
console.log(true && true); //true
console.log(true && false); //false
console.log(false && true); //false
console.log(false && false); //false
Since any type of variable can evaluate to true or false, the above logic can be extended as -
console.log(Boolean(1) && true); //true
console.log(Boolean(0) && true); //false
console.log(1 && Boolean("abc")); //true
I am using ‘Boolean’ to ‘cast’ the value to a boolean and print ’true’ or ‘false’. This is simpler while doing actual comparison.
See type casting in Javascript to know more. Or, just Google it - that is what I would have done 😜
if (1 && "abc") {
console.log("abc stands true");
}
// output: abc stands true
One of the best things of an AND is that Javascript never evaluates the second expression if the first expression is false.
So, you will find code like the below.
function getAnswerToSomething(something) {
if (something && something.length > 0) console.log("awesome");
else console.log("nothing exists");
}
getAnswerToSomething("abc"); // awesome
getAnswerToSomething(""); // nothing exists
The code fragment errors out if something was null and the second expression was moved to the first place.
function getAnswerToSomething(something) {
// not really a good idea
if (something.length > 0 && something) console.log("awesome");
else console.log("nothing exists");
}
getAnswerToSomething(null); // error: Cannot read property 'length' of null
OR
Everything is ture in the ‘OR’ world if at least one of the expressions is true.
So:
console.log(true || true); //true
console.log(true || false); //true
console.log(false || false); //false
Extending this further:
console.log(Boolean(1) || false); //true
console.log(Boolean(0) || true); //true
console.log(0 || Boolean("")); //false
OR will not evaluate the second expression if the first one is true. Regardless of the second expression, the overall result will be true if the first expression is true. (And, the same holds good for false as well).
NOT
Finally, we come to the negation of/to everything. NOT operator just turns the result on its head - true to false, and false to true.
console.log(!true); //false
console.log(!"abc"); //false
console.log(!0); //true
You can see that ! is turning everything to Boolean.
And, you already know that ‘opposite of opposite of something’ is ‘something’ represented as simply true or false.
Combining the two, we can rewrite expressions used earlier in a more friendly way instead of the explicit Boolean typecasting.
console.log(!!1 || false); //true
console.log(!!0 || true); //true
console.log(0 || !!""); //false
Go Wild
Logical and comparison operators form one hell of a team. All it takes here on is for you to go wild with the various permutations and combinations of any and all operators.
A glimpse of what is possible:
console.log((!"" && true && !!" is the word ") || (!!null && !!"said none")); // true
Cheerio.