Logical Operators

Logical operators are used when I have more than one input and I need to decide how to combine those inputs. A simple logical operator example would be deciding to buy electronics. You have three basic options: quality, cost, age. Logically (I wanted to say obviously, but having worked B2B this might not always be that obvious to everybody) you can't have all three at the same time, so you make a couple choices. Here's a basic look at some of our thinking process to make this very logical decision:

  1. Quality and Cheap. It's possible to have one AND the other, as long as I'm happy with an older model.
  2. Quality, Cheap and New. Unlikely, it's going to have to be one OR the other.
  3. Cheap and New. Well, then it's probably NOT going to be Quality.

That's exactly what logical operators do. Javascript has those three:

And Or Not
&& || !

Instead of re-inventing the wheel I'm pasting the combo examples from Codecademy's lesson here, it's fairly straighforward.

And

true && true;   // => true
true && false;  // => false
false && true;  // => false
false && false; // => false

Or

true || true;   // => true
true || false;  // => true
false || true;  // => true
false || false; // => false

Not

!true;  // => false
!false; // => true

As a test run I'm going to write the top example as some Javascript using logical operators.

var quality = prompt("Are you looking for high quality Electronics?","Yes or No");
var age = prompt("Must it be new, or would you be interested in taking a look at our used models?","New or Used");

if (quality === "Yes" && age === "New") {

    var price = prompt("How many dollaroos are you thinking of spending?");

    if (price <= 2000) {
        console.log ("You might be better served at Best Buy.");
    }
    if (price >= 5000) {
        console.log ("We have some excellent Tandberg's that just came in. Let's take a listen in our sound booth.");
    }
    else {
        console.log ("I'm sorry, we don't stock any new models in that price range. Maybe I can interest you something a little older?");
    }
}
else if (quality === "Yes" || age === "New") {

    var price = prompt("What's your budget?");

    if (price <= 2000) {
        console.log ("You might be better served at Best Buy.");
    }
    else if (price >= 5000 && quality === "Yes") {
        console.log ("For that we can get you some really nice new equipment, or we can take a look at this incredible, vintage system.");
    }
    else if (price <= 5000 && quality === "Yes") {
        console.log ("You might prefer looking at used models. I have a used Bang & Olufsen you might beinterested in...");
    }
    else if (price >= 5000 && age === "New") {
        console.log ("If you like Beomaster I have just the thing for you. Follow me into the sound booth.");
    }
    else {
        console.log ("You might prefer looking at used models. I have a used Bang & Olufsen you might beinterested in...");
    }
}
else {
    console.log("You're probably looking for the thrift store? They're round the corner.");
}