There are four logical operators that we'll cover: or (
||), and (
&&), not (
!), and nullish coalescing (
When you use the or (
||) operator, you're telling the program to do something if this value is true, or if that value is true. As long as one of them evaluates to true, you'll be happy.
When you use the and (
&&) operator, you're telling the program to do something if this value is true and if that value is true. Both values need to evaluate to true.
When you use the not (
!) operator, you're looking for the opposite value of what you're comparing. So if you say "not true", you're looking for the value to be false.
The nullish coalescing operator is similar to the or operator, but instead of moving to the second statement if the first statement has any falsy value, it only moves on if the first statement is
&&, you can actually have more than two comparisons that you're looking at, but you need at least two. The list of comparisons can be as long as you want or need it to be. With
||, you only need one of them to evaluate to true and with
&& you still need all of them to evaluate to true.
There are two sets of comparison operators, equality/inequality and relational.
Equality and inequality comparison operators check to see if one value is equal to another value, or if it is not equal to another value. Equality can be checked by using
===, and inequality can be checked by using
!== are doing a strict comparison, whereas
!= are doing an abstract comparison. We'll cover that in future videos.
Relational comparison comparison operators are checking to see if one value is greater than (
>), greater than or equal to (
>=), less than (
<), or less than or equal to (
<=) another value. These work just like they did in your math class. But don't worry, you don't have to have been good at math to understand this. 😃
For more background on logical and comparison operators, you can read the following blogposts: