Operators

Logical Operators

🙋 Need help? Ask an expert now!

Logical operators take boolean operands and return a boolean result. There are few important logical operators to remember. The operator !, is the logical not operator, which turns true values to false and false values to true.

The && operator is the logical and operator, which takes in two boolean values and returns true only if both of the values are true or false. The four different cases are shown below:

  1. true && true - evaluates to true
  2. true && false - evaluates to false
  3. false && true - evaluates to false
  4. false && false - evaluates to true

The || operator is the logical or operator, which takes in two boolean values and returns true if one of the values are true. The four different cases are shown below:

  1. true || true - evaluates to true
  2. true || false - evaluates to true
  3. false || true - evaluates to true
  4. false || false - evaluates to false

As JavaScript is a weakly typed language, any value can turn into true or false values. Every data type can be converted to it's boolean representation using double not (!!).

As shown in the sample code above, logical operators are extremely useful and crucial for almost any program. Logical operators can even be used together as shown in the fourth console.log() statement above. True-like or false-like values represent other data types that are converted to true or false using !!. Knowing which data types result in true or false is not as important as knowing how to use the logical operators, so focus on mastering logical operators before diving into true-like and false-like values.

Edit Me on GitHub!

Application Question

Which of the following statements evaluate to true?