Drag the edges to resize the window.

Code Editor
Control flow

Logical Operators

So far, we've been able to translate certain thoughts into JavaScript code, like, "Are these things equal?" with ===, or, "Is one thing greater than another thing?" with >.

In English, sometimes we say "both of these things" or "either one of these things." Let's translate those phrases into JavaScript with some special operators called logical operators.

  1. To say "both must be true," we can use &&.
  2. To say "either can be true," we can use ||.
  3. To say "I want to make sure this is the opposite of what it really is," we can use !.
  4. To say "these should not be equal to each other," we can use !==.

For example:

if (stopLight === 'green' && pedestrians === false) { console.log('Go!'); } else { console.log('Stop'); }
  1. In the example above, we make sure that the stopLight is 'green' and && there are no pedestrians before we log 'Go!'.
  2. If either of those conditions are false, we log 'Stop'.

Just like the operators we learned previously, these logical operators will return either true or false.

These logical operators are helpful when writing if/else statements, since they let us make sure multiple variables are true or false.

Report a Bug
If you see a bug or any other issue with this page, please report it here.