3.7 Les Opérateurs logiques
Hello everyone and welcome to this video where I'm going to show you a little demonstration on logical operators. We use these operators to make decisions based on multiple conditions. In JavaScript, we have 3 types of operators, the and operator which means '(AND)', the or operator '(OR)', and the operator '(NOT)' (is not) operator. Let's see each of these operators in action. So I'll start with the logical 'AND' which is indicated by two ampersands. Here is the basic rule. This 'AND' logic returns true if and only if both operands are true. What do I mean by this ? Well let's do a console log of true and true, we have no 2 operands so they are both true. So the result of evaluating this expression will be true. Let's take a look at the console, we have true. Now if one of them is false, the result will be false. I will change this to false, we save the changes and we get false on the console. It doesn't matter which one is false, if I change one of the two operands to false or both to false I'm still in false. So the 'AND' logic returns true if and only if both operands are true. Now you may wonder what is the real use case for this operator? Well let's say we want to create a loan approval application and we want to see if the applicant has a high income and a good credit rating, then he will qualify for loans. So first I'm going to remove all of that and declare some variables that we're going to call 'highInComes' which is high income and then set it to true. Then we'll declare another variable that we'll call 'goodCreditScore' which corresponds to a good credit rating that we'll also set to true. Here we are dealing with two conditions, we want to make sure that the applicant has a high income and a good credit score, this is where we use the logic 'AND', we can then declare another variable that we will call 'eligibeForLoan' which means eligible for the loan. This is where we use the logical operator 'AND'. We make a 'highIncomes'. AND (2 ampersands). Now if we make a console.log of 'eligibleForLoan' we record, we get true. Now let's take a look at the 'OR' logic, the 'OR' logic or the 'OR' logic is indicated by 2 vertical lines and returns true if and only if one of the operands is true, it doesn't matter which one, whether it's on the left or on the right or both, as long as we have an operand that is true the result of this expression will be true. So here's an example, we're going to replace this 'AND' logic with the 'OR' logic, now in this case both operands are true, the result of this expression will be true too. Now if I set one of them to false, we always get true, it doesn't matter which one is true, as long as we have an operand in this expression that has the value true, the result of this expression will also be true. So this is how we use the 'OR' logic Finally let's look at the 'NOT' operator, it is indicated by an exclamation mark. Let's imagine that the applicant is not eligible for the loan, you want to consider the application as refused, so we'll declare another variable that we'll call 'refuse' and here we use the 'NOT' operator that we apply to eligibleForLon. So this is what happens here, if the loan is eligible, this operator will not convert it to false. So whatever we give it, it will give us the opposite, in this case if eligibleForLan is true, it will convert it to false and refuse will be false. Because if someone is eligible for a loan, we don't want to deny their application, so the denied application is always the opposite of eligibleForLoan and that's where we used the 'NOT' operator. So let's see this in action. We're going to change these two conditions to false, so we're dealing with someone who has a low income and a bad credit score, obviously he's not eligible for a loan, so we record that on the console and we can also add a label so we're going to add a string that we're going to call 'eligible'. Here we add a comma. We apply the operator 'NOT', to define the variable refuse. We make another console log of refuse and here we add a string that we will put 'application refuse'. We save the modifications and we see on the console that the applicant is not eligible, because eligible is false and the request to refuse is true because it is always the opposite of eligible to the loan. That's it for this demonstration on logical operators We meet again for the next video where we will talk about logical operators with non-Booleans.