How and when is the && || logical operators supposed to be used?


#1

When should I use the and/or logical operators? How should I use them? Thank you. :grin:
Also, where can I incorporate them into this if/else statement? I am not sure if my if/else statement is very correct, either.

    case 'yes':
        if(user == YES) {
            console.log("Well, duh");
        }
        else {
            console.log("YUMMY YUMMY");
        }
        break;

#2

You could for example do:

if(user == "yes" or user == "YES")

this way, you cover both lowercase and uppercase cases. Careful here:

if(user == YES)

YES has no quotation marks ("), so it is a variable, sure this shouldn't be a string?


#3

You're mixing two different things, a switch statement, and an if/else statement. A switch statement here...

switch(something) {
    case "Something":
    case "something":
        console.log(something);
        break;
    default:
        console.log("Nothing");
}

And the same thing in a if/else statement...

if (something == "something" || something == "Something") {
    console.log(something);
} else {
    console.log("Nothing");
}

Also, you can use && like so...

if (a = "A" && b = "B") {
    //do something
}

#4

Your if/else statement looks good to me. You could use what @stetim94 suggested to make use of the or operator, but I'm not sure if you have enough information to use the and operator just by looking at this piece of code. If you have to, you could prompt the user to give you another piece of information, like an age, and use that in your if/else statement.
For example:

var age = prompt("What's your age?");
case 'yes':
if(user == YES && age == 20) {
console.log("Well, duh");
}
else {
console.log("YUMMY YUMMY");
}
break;

You could also just repeat user == YES after the &&, but that seems a bit repetitive.
I also don't think you need to use a switch statement here (unless prompted by the lesson).