&&
- 'and'
&&
- means 'and' and both the left-hand side and the right-hand side operands must be 'truthy' for &&
to be 'truthy.'
For example, if I tell my daughter that she will receive ๐ฆ if she gets an 'A' on her test and cleans her room, then both of those conditions must be true
for her to get ๐ฆ.
Here's how that might look in code:
const daughter = {
name: "Lily",
devToUsername: "@awesomecoder123"
mostRecentTestScore: "A",
roomStatus: "Clean"
}
if (daughter.mostRecentTestScore === "A" && daughter.roomStatus === "Clean) {
// This will only 'log' if BOTH CONDITIONS are 'truthy'
console.log("๐ฆ");
}
And, here are a couple of examples loosely referencing how this sort of thing might work in React:
&&
, if the left-hand side operand is 'false-y,' then there is no reason to evaluate the right-hand side operand.
If the left-hand side operand is 'false-y,' then we shortcircuit the &&
.
Using the analogy above ๐๐ฝ, if my daughter didn't receive an "A," there isn't any reason to check her room with regards to ๐ฆ.
||
- 'or'
||
- means 'or' and either the left-hand side or the right-hand side operand must be 'truthy' for ||
to be 'truthy.'
Using the same 'daughter analogy' ๐๐ฝ, if the deal was that she could get ๐ฆ if either she got that "A," or she cleaned her room...
const daughter = {
name: "Lily",
devToUsername: "@awesomecoder123"
mostRecentTestScore: "A",
roomStatus: "Dirty"
}
if (daughter.mostRecentTestScore === "A" || daughter.roomStatus === "Clean) {
// This will only 'log' even if the 'room is dirty' b/c she got the 'A' ๐
console.log("๐ฆ");
}
And, here's another example showing short-circuiting of an ||
. This means that if the left-hand side operand is 'truth-y,' we don't bother to look ๐ at the right-hand side operand - we short-circuit the ||
.
Finally, we see one somewhat more realistic example putting things back together.
Top comments (2)
Well where's my ice cream dad?
๐ We'll C what happens. If you pass the
&&
conditional operator. ;)