Javascript Fundamentals - Part 4(Logical operators)

Javascript Fundamentals - Part 4(Logical operators)

Hello, curious student on the internet, today we are going to learn about logical operators in Javascript. If you have covered Boolean logic in your classes at any time, then this is the same as using logic gates in programming. In Boolean logic we used to have and operator, or operators and negation operator. Logical operators are the same thing just implemented using JavaScript.

AND operator

The first logical operator is AND. It is used to compare two conditions at the same time. Let's say I have to compare two numeric values. Let's just go with the variable x, which has a value of 7. And I have another variable y which has a value, let's say, 10. If I have to compare that if x is less than 10 and y is greater than 9, then print out some statement, then I will use and operator as shown below:-

const x = 7;
const y = 10;

if(x<10 && y>9) {
  console.log("Test was passed");
} else {
  console.log("Test failed");
}

// Output:- Test was passed

The operator in the if block is called and operator. We use double amper's and(&&) to represent AND operator in Javascript.

Now, if you try to run this code. We have test was passed as output because x value is 7 and it is less than 10. Also, the y value is 10 which is greater than 9.

Now, if any of these conditions failed like let's say y is 8 instead of 10 and we take a value that is less than 9. And if you run this code. Our test will fail as shown below.

const x = 7;
const y = 8;

if(x<10 && y>9) {
  console.log("Test was passed");
} else {
  console.log("Test failed");
}

// Output:- Test failed

If both the conditions are true or both pass, only then the and condition will be passed and the code will execute.

OR Operator

Let's continue with the next operator, which is OR operator in Javascript. OR is represented by || notation in Javascript.

Example:-
Let's assume x is 7, and y is 8. We are checking whether x is less than 10 or y is greater than 9

const x = 7;
const y = 8;

if(x<10 || y>9) {
  console.log("Test was passed");
} else {
  console.log("Test failed");
}

// Output:- Test was passed

So in this case, if we try to run the code, the test case will pass since the first condition is true for the if block. The difference between OR and AND is, in the case of OR any of the conditions needs to be true for the condition to pass. So in this case, even if one of the condition is true, the test will pass.

OR operator will fail or return false, only if both the conditions are failing as shown below:-

const x = 17;
const y = 8;

if(x<10 || y>9) {
  console.log("Test was passed");
} else {
  console.log("Test failed");
}

// Output:- Test failed

Combining AND/OR operator

We can also combine AND with the OR operator as shown below:-

const x = 5;
const y = 5;
if(x<8 && (y>18 || y==0) ) {
  console.log("combined test passed");
} else {
  console.log("combined test failed");
}

//Output:- combined test failed

Here x value is less than 8 so the first condition for AND will pass and it will move towards the second test. In the second condition, we are using the OR operator to check two comparisons. Since the y value is neither greater than 18 nor equal to 0, so OR operator will return false as the output. In the case of the main AND operation since the second condition fails so the result of the operation is returned as false.

NOT operator

NOT operator is another logical operator which you can use, and basically, it converts, anything that you pass to it to the opposite value. For example, if you are trying to check whether a particular value is equal to something or if it is true or not, NOT will invert the result for us in equality checks.

NOT operator is represented by !(exclamation mark) in Javascript.

I'm just going to take a very basic example to explain this first. So let's say we have a const boolean value, which is set to false. And I'm using the if condition to check the reverse of the value:-

const booleanValue = false;
if(!booleanValue) {
  console.log("Condition passed");
} else {
  console.log("Condition failed");
}

// Output:- Condition passed

For the next example, let's say we have a variable x set to value 9. In the if block we check whether x is equal to 9 and negate the result using NOT operator as shown below:-

const x = 9;
if(!(x==9)) {
  console.log("Condition passed");
} else {
  console.log("Condition failed");
}

// Output:- Condition failed

In this case, x==9 returns true which is then inverted by NOT operator to false. This is why else condition runs and the output is Condition failed.

We will continue learning more about Javascript language in the upcoming articles in the series. I hope you found the article useful.
Which is your favourite feature of Javascript language? Do share it in the comments below.
I create content about building web applications and general programming. If this is something that interests you, please share the article with your friends and connections. You can also subscribe to my newsletter or follow me to get updates every time I write something!

Thank you for reading, if you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

I would love to connect with you on Twitter | Instagram.

You should definitely check out my other Blogs:

See you in my next article, Take care!!

Did you find this article valuable?

Support saurabhnative by becoming a sponsor. Any amount is appreciated!