Skip to content

Logical operators

We sometimes have to make decisions based on complex conditions. We'll discuss how to use logical operators to express these complex conditions. We'll also learn how the concept of truthy and falsy values relates to these operators.

SlidesPDF

  1. This exercise tests your understanding of how logical operators work with truthy and falsy values. Read the following code and try to answer what each console.log will output without running the code:

    const user = {
      username: 'khadija.g',
      name: 'Khadija',
      bio: null,
      bookmarks: [],
    };
    
    console.log(!user.bookmarks);
    console.log(!(!user.bookmarks));
    
    console.log(user.name && user.name.toUpperCase());
    console.log(user.address && user.address.city);
    
    console.log(user.username || user.name);
    console.log(user.bio || 'No bio');
    

    Run the code afterwards to verify your answers.

  2. Filenames typically end in suffixes like “.pdf”. These suffixes are called file extensions and they’re used to indicate the type of file. The “.pdf” in “logic.pdf” indicates that logic.pdf is a PDF document.

    Some file types have several file extensions. A Word document may have a “.doc” extension or a “.docx” extension.

    The following table lists some file types and their associated file extension(s):

    File typeFile extension(s)
    Word document.doc, .docx
    Excel worksheet.xls, .xlsx
    PowerPoint presentation.ppt, .pptx
    PDF document.pdf

    Using the table, complete the following program so that it prints the file type of the filename variable. The variable is currently set to 'slides.pptx', so the program should print a message like slides.pptx is a PowerPoint presentation. If you change the value of filename to a different string like 'ebook.pdf', then the program should print something like ebook.pdf is a PDF document. However, if you change filename to a value like 'photo.jpg' which has a file extension that’s not in the above table, then your program should output a message like The file type of ‘photo.jpg’ is not known.

    Ensure to use a logical operator to test for file types like “Word document” that have multiple extensions.

    const filename = 'slides.pptx'
    
    // Your task: complete the program.
    

    Hint: You can test if a string has a given suffix with the string .endsWith method.

Remember that the if statement is a statement. It’s not an expression so it doesn’t produce a value. Why’s this important? Well, consider the following piece of code, where we assign a value to the remark variable depending on some condition (the condition doesn’t really matter; what matters is how we write the code):

let remark;
if (condition) {
  remark = "yes";
} else {
  remark = "no";
}

Rewriting it as follows is invalid because we’re treating the if statement like an expression:

let remark = if (condition) {
  "yes";
} else {
  "no";
}

However, it is sometimes convenient to use an expression to make a decision. For this, JavaScript has a conditional operator. It looks like this:

expression1 ? expression2 : expression3

Here’s how it works: if expression1 is truthy, then the result of the operation will be the value of expression2. But if it’s falsy, the result will be the value of expression3.

We can rewrite the if statement example as below:

let remark = condition ? "yes" : "no"

If the condition is truthy, the remark variable will be assigned the value "yes"; if it’s falsy, remark will be assigned "no".

Many operators in JavaScript, like the addition operator (+), the OR operator (||), and the AND operator (&&), take two operands, as in:

2 + 3;
true || false;
'hi' && 'hey';

Thus we call them binary operators.

Others like typeof and NOT (!) that take a single operand are unary operators. The conditional operator (? :) is the only ternary operator, taking three operands.