Logical Operators | Bondar Academy
Course: JavaScript for Testers
Module: JavaScript Fundamentals
Instructor: Artem Bondar
Lesson Summary
This lesson covers logical operators , which are essential for processing logical expressions in programming. The primary logical operators discussed are AND , OR , and NOT . Logical AND Operator The AND operator, represented by && , combines two or more logical expressions. The entire expression evaluates to true only if all individual expressions are true . For example: console.log(true && true); // Outputs: true console.log(true && false); // Outputs: false Logical OR Operator The OR operator, denoted by || , evaluates to true if at least one of the expressions is true . For instance: console.log(true || false); // Outputs: true console.log(false || false); // Outputs: false Real-World Example Consider a scenario where we check if a user is eligible for a driver’s license. The criteria are: The user must be a US citizen . The user must be over 18 years old . The logical expression for eligibility can be constructed using the AND operator: let isEligible = (age > 18 && isUSCitizen); Logical NOT Operator The NOT operator, represented by ! , inverts the truth value of an expression. For example: console.log(!true); // Outputs: false console.log(6 != 10); // Outputs: true Summary In summary: AND : && - All values must be true. OR : || - Any value must be true. NOT : ! - Inverts the truth value. These operators are fundamental for building logical expressions in programming.
Video Transcript
In this lesson, we will talk about logical operators. Logical operators are used when you need to process the logical expressions. For example, you want to combine several logical expressions or compare several logical expressions. And those logical operators are and, or, or not. So in this lesson, I will show you how to use and evaluate those logical expressions and use those statements. So let's get into it. So let's begin with logical and operator. So how to define this? Let's do this console.log. And I will type true and true. So what does it mean? Logical operator and, which is defined by double ampersand, is combining two or more logical expressions together. And this can be defined as all values have to be true. For expression to be true. So we have two expressions, on the left and on the right. And with the logical and operator, we combine them together, defining that the entire expression will be as a true. So let me run this and show you real quick. And we see that this evaluation is evaluated into the true. If I will change any of this into the false and try to run it one more time, we see that right now it's printed in the console as false. Because one of the evaluation is false. And if we combine true and false, the entire expression is equal to false. And as an opposite, another logical operator, logical or that behaves the opposite way. Let me copy and paste it over here. And this can be defined as any value should be true for the expression to be true. Which means that if I will run this expression right now, by the way, or is defined by double pipe, like this. And if I will run it this time, we see that this one is evaluated into the true. We have true on the left or false on the right. And the entire expression is evaluated into true. If I will have false on both sides, only in this example, the entire expression will be evaluated into the false. I know right now this sounds confusing. So let me show you the example to clarify how this work on real life example. So let's assume we develop in an online form that will determine is the user is eligible for receiving a driver license. And according to our requirements, we know that our user should have a US citizen and should have age more than 18. In this case, this user can be eligible or this customer can be eligible for a driver license. So let's create two variables. One will be age is more than 18. And the second variable will be is US citizen. Okay, this is our requirement. And now we create the logical expression. All right, so that's the code that we created that will check our logic and what we see in line number 10. So eligibility for driver license will have the final result. Is this customer eligible or not? And this final result will be determined by this expression, that this user should be more than 18 years old and this user should be a US citizen. And we need to provide this expression in order to And we need to provide a Boolean values. So if we provide here true, that our user is more than 18 years old. And true, that our user is US citizen. And then we run our code right here. We see in the console we printed, this customer is eligible for driver license is true. But let's say that our user filling up this form and it's less than 18 years old. Let's say this user or this customer is 16. In this case, this will be false, right? And when we run this application one more time, we see what? This customer is eligible for driver license is false. Why? Because our customer didn't meet their required criteria to be eligible. So it is a US citizen, but unfortunately, it's less than 18 years old, which is here. And as a result, our customer is not eligible for the driver's license. Going back one more time. And operator works the way that it combines several logical expressions together and define the final result of this expression. So and will be evaluated into the true if all values of the expression evaluated into the true. And let's compare the other approach. Let's say we develop in a form which says like, we can give the driver license to any US citizen. Or we can give a driver license to any who is more than 18 years old. In this case, we can use or operator. It is double five. In this example, if our user is more than 18 years old, sorry. In this example, our user is less than 18 years old because it's false. And if US citizen is false as well, then it's gonna be what? It's gonna be false. But if our user is just a US citizen, he can get the driver license no matter age it will have, right? Or if it is not US citizen, but it is more than 18 years old, it is also eligible for the driver license. This evaluation also has expression as true. So one more time, with or operator, the entire expression will be evaluated into a true if any of the values inside of the expression is true. Then the entire result will be a true. So this is how this kind of logical operators work in real world scenario. And one more example is logical not operator. Logical not operator is the opposite of true. So if I type something like console.log true, exclamation sign, so exclamation sign is a logical not operator. So the opposite from the value which is inside. And if I run this, we will see that not true is false, right? And let's say one more example, console.log. And we say, let's say, 6 is equal to 10, right? And if I run this code right now, we see that definitely it is false, right? Because 6 is not equal to 10. But if we want to write down something, is 6 not equal to 10? Like they ask the question the other way around, from the opposite side. Is 6 not equal to 10? Yes, it's not equal. And if we evaluate this, it will say as true, because 6 is not equal to 10, and this is why it is true. Logical not operator is the opposite one. Sometimes if you need to build your logic from the opposite direction, you can use this exclamation sign, which is represent a not operator, a logical not operator to build the expression with the result that you need. All right, so let's quickly summarize what we did in this lesson. We learned a logical operators. Logical and is defined by double ampersand. And all values have to be true for the expression to be true. Logical or operator is defined by the double pipe sign. And any values should be true for the expression to be true, which is opposite from and operator. And logical not operator is in form of the exclamation sign, which is opposite from the true. If you need to build a negative expression with the positive expression, you just use exclamation sign and it will reverse the logic of the expression for you. All right, that's it guys, and see you in the next lesson.