Need help understanding Logical operators in JS


#1

Can Someone explain to me Logical Operators and the reasons we use them for , thanks



#2

You had another topic that talked about this slightly, but which ones are you having trouble understanding?
Do you understand the basic ones like AND (&&) and OR (||)?


#3

I need a basic explanation of the reasons of why we use them and why they are helpful and what they do


#4

what they do should be explained in the lesson? they allows us to check multiply conditions at a single if statement, loop or anything that uses condition checking

You are going to build a rock paper scissors game later, if you want to validate the user inputted the correct thing:

do {
   userChoice = prompt("enter choice: ");
} while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")

by using && you can check in one condition if the choice is correct, rather then 3 different conditions, this is just one example, you will see if you continue the course that you will use this more often.


#5

Okay, well we use them, mostly in conditional statements, to return a true or false value.
For example, you would use them in a conditional like this:

if (shoes == "yes" && socks == "yes") { //yes should've been a string, thanks stetim94 for pointing it out
    console.log("Good job keeping the feet not smelly");
}

The reason this works is because the AND operator checks both sides of the and sign, then determines whether it's true or false.
This is just a simple demonstration of where you would see them, and what it would look like in a conditional statement.
They aren't just helpful, they're necessary to test things, and the more complicated your programs get, the more you'll see them.

Here is a related resource from Mozilla:

if you have any more specific questions, i might have an easier time answering something that you're confused about specifically, rather than a generic "why does this help" because i don't know exactly what you know and it would take a long time to explain


#6

Why didn't you continue this on your other post? :confused:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.