Check out a free preview of the full Introduction to JavaScript course:
The "And & Or Operators" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian introduces && and || comparison operators for use in control flow statements.

Get Unlimited Access Now

Transcript from the "And & Or Operators" Lesson

[00:00:00]
>> Brian Holt: Talking more about conditionals, let's actually just go over to our VS Code here. I think this will be a little bit easier.
>> Brian Holt: So.
>> Brian Holt: So let's just get rid of that. There's a couple more things about conditioners that I should show you really quick, so when I was just showing you if two is not equal to three than console these are not equal, right, or we'll just say like const.age = 25, and const.

[00:00:54]
>> Brian Holt: Like ageToDriveARental car.
>> Brian Holt: So in this case you'd probably have to do something like ageToDriveARentalCar, whoops.
>> Brian Holt: And this would be 25, and let's just put this as like 28.
>> Brian Holt: Then here you would say like, you are not old enough to drive a car.
>> Brian Holt: All right we talked a little bit about that.

[00:01:41]
>> Brian Holt: But yeah, something else you can do with the not equals two, which was yeah, let's just get rid of that example, so we'll just stick to x and y for us. I can't think of a real example off the top of my head. So if x is not equal to y, then console.log('these are not equal').

[00:02:08]
>> Brian Holt: We'll put another z up there, const z = 11. So two of them that I did not show you yet are or and and, so if I say if x is equal to y and, and the way you do and is with two ampersands next to each other, and y is equal to z.

[00:02:39]
>> Brian Holt: Console.log.
>> Brian Holt: These are all equal. So this is what's called a logical end, right? And here, this means, if this is true, and this is true, then do this, right? Make sense? And you can have as many of these as you want.
>> Brian Holt: Right.
>> Brian Holt: Now, there's another one called or, and the way you do that, so if is true, or x triple equals z,

[00:03:34]
>> Brian Holt: This is.
>> Brian Holt: Okay, and so this is two pipes, oops, right there, two pipes next to each other.
>> Brian Holt: So it's gonna ask the question, if this is true or this is true then this is going to run. So in this particular case, since Is true is true, despite the fact that this is not true over here, this will still run, right?

[00:04:02]
Whereas up here, as soon as it sees the x is not equal to y, it's actually not even going to evaluate the rest of this. It's just like okay, well, the first one wasn't true, so I'm not gonna do that, so all of them have to be true.

[00:04:15]
And
>> Brian Holt: You can combine these using parentheses, so.
>> Brian Holt: Right, so it's gonna say, if this is true, or both of these are true, then do this. Once you start getting into compound conditions and this starts getting a lot harder to read, you should start re-evaluating what you're trying to do there.

[00:04:49]
Normally it can be rewritten into a more simple way either by breaking that into multiple statements or just being a little bit more explicit, but when you start shoving so much in there it gets really tough to figure out what the hell is going on.
>> Brian Holt: Cool, any questions?

[00:05:09]
>> Speaker 2: On line 11, you said if x doesn't equal y then it's gonna ignore this stuff to the right. If you replace the second and with an or would it still ignore it? No, the second one.
>> Brian Holt: The second one.
>> Speaker 2: Yeah.
>> Brian Holt: So if I do this.

[00:05:30]
>> Speaker 2: Because that should return true, because if it's true is true.
>> Brian Holt: Yeah, you know the question here which I would have to go figure out is what is the priority of Compton tutorials here, right? Is it asking is this and this true? Is it asking is this or this true?

[00:05:49]
>> Speaker 2: It enforced parentheses there.
>> Brian Holt: Yeah, so if I put parentheses here. Do you wannna put parentheses here?
>> Speaker 2: Sure.
>> Brian Holt: Okay, so if it does this then it's going to evaluate this whole block. It's gonna find out that this is false and then it'll evaluate this one.

[00:06:09]
>> Brian Holt: But so if I did it like this,
>> Brian Holt: It's going to evaluate this. It's gonna see this is not true and not abide with the second term.
>> Speaker 2: Because it recognizes that the rest of the stuff is inside of parentheses and so it doesn't matter?
>> Brian Holt: Right.

[00:06:28]
>> Speaker 2: Okay.
>> Brian Holt: No matter what, if it see this is not true and then it sees and right afterwards it means it doesn't matter what comes afterwards because the first term is already false.
>> Speaker 2: But if it does let you do those same functions in one line, that seems like it would be bad to just ignore the stuff on the right, in case there is an or there.

[00:06:55]
>> Speaker 2: It's like you said. I guess the question is, does it enforce [COUGH] parentheses or does it let you-
>> Brian Holt: Got you. It does let you enforce, so the parentheses will still work. So is there another one after this right?
>> Brian Holt: And at this point I'd say you need to refactor your code [LAUGH].

[00:07:14]
Yeah, this will work for sure cuz it will evaluate something else at this point.
>> Speaker 2: It will, okay.
>> Brian Holt: Yeah.
>> Brian Holt: Good question, and you start getting into these deeper semantics. There's not really a ton of point learning what that does. I know people who disagree with me, but there's enough stuff in my brain, like lyrics to 90s songs, that I just can't afford to lose anything else up there [LAUGH]