&& (Logical AND)
Imagine that you are having a party and want to have some rules about who is allowed in.
You might want to only allow people who are wearing glasses AND who are over 18 to be
allowed in. This is an example of a logical AND condition: anybody coming to the party
must satisfy both conditions before they are let in.
The logical AND operator works on two or more values (the operands) and only evaluates
to true if all the operands are truthy. The value that is returned is the last truthy value if
they are all true, or the first falsy value if at least one of them is false:
true && true;
3 && 0; // returns 0 because it is falsy
|| (Logical OR)
Now imagine that you relax the rules for your party and allow people in if they wear glasses
OR are over 18. This means that they only have to satisfy one of the rules to be allowed
in―an example of a logical OR condition.
The logical OR operator also works on two or more operands, but evaluates to true if any
of the operands are true, so it only evaluates to false if both operands are falsy. The value
that is returned is the first truthy value if any of them are true, or the last falsy value if all
of them are false:
true || false;
NaN || undefined;
↵ // both NaN and undefined are falsy, so undefined will be
Search WWH ::