Logical Operators
To understand how logical operators actually work, let's use the boolean variables from our last lecture :
const isASoftwareEngineer = true // Refers to X
const livesInParis = true // Refers to Y
We are going to start with the AND
and OR
operators.
The AND
operator in JavaScript is represented with a double ampersand symbol ( &&
). Here is an example :
// true AND true = true
console.log(isASoftwareEngineer && livesInParis) // Result ==> true
The OR
operator in JavaScript is represented with two vertical bars ( ||
). Let's change the value of livesInParis
to false
and see the result of the below operation.
// true OR false = true
console.log(isASoftwareEngineer || livesInParis) // Result ==> true
The result is indeed true
; because if you remember, with the OR
operator, it's enough for one of the variables to be true
for the whole operation to become true
.
The NOT
operator is you already know is represented by the exclamation mark ( !
); changing any boolean variable from true to false or false to true.
// NOT true = false
console.log(!isASoftwareEngineer) // Result ==> false
Now that we know how all these operators work, we can now use these variables to make decisions. Let's say we want to determine if our user Jacob can be a software developer in Paris. As mentioned in the last lecture, we can now use boolean variables and boolean logic to model complex situations.
const canBeASoftwareDeveloperInParis = isASoftwareEngineer && livesInParis
/**
* You can directly do this : if(isASoftwareEngineer && livesInParis)
*/
if(canBeASoftwareDeveloperInParis) {
console.log('Jacob is fit to be a Software Developer in Paris')
}else {
console.log("Jacob can't be a Software Developer in Paris")
}
If you give this a try, you will notice that it is the else block
that logs in the console. We have this result because, if you remember, we changed the value of livesInParis
to false
, making our condition canBeASoftwareDeveloperInParis
to be false
. Our condition canBeASoftwareDeveloperInParis
can only be true if both variables are true; meaning if we want the if block
to be executed, we need to change back livesInParis
to true
.
Let's take this a little further by adding a third boolean variable :
const isRetired = false // Refers to Z
We will improve our decision-making to check whether Jacob should work as a software developer or not. With this third variable, we want Jacob to be able to work as a software developer if he is a Software Engineer
, if he lives in Paris
, and if he is not retired
.
Let's try to translate that into a condition. Up till now, what we have in our if
condition is he needs to be a software engineer and should live in Paris. But now, we want him not to be retired. Right now, he is not retired ( isRetired = false
), and that, of course, meets all the requirements for Jacob to be a software developer. Here is what our code should look like to have the expected result :
if(isASoftwareEngineer && livesInParis && !isRetired) {
console.log('Jacob is fit to be a Software Developer in Paris')
}else {
console.log("Jacob can't be a Software Developer in Paris")
}
Remember that for !isRetired
to be true
, isRetired
needs to be false
because the NOT
operator gives us the opposite of our current value. Meaning if we want Jacob to be a developer, we need isRetired
to be false so that the NOT
operator turns it to true
, giving us; true AND true AND true
which results to true
. Thus executing the if block
.
As you can see, with these boolean operators, we can model all kinds of complex decisions like we just did.