Friday , 17 November 2017
Home / Programming / JavaScript / Logical Operators – JavaScript Tutorial
Logical Operators
Logical Operators

Logical Operators – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

In this tutorial, we’ll be discussing what logical operators are. Logical operators allow you to define multiple conditions in one conditional statement. Let’s see how to do this.

Using Logical Operators

Clear your script and add this bit of code.

Let’s focus on the if statement here. You’ll notice we have a && also known as a double ampersand.  This tells your conditional statement that you have 2 conditions that must be true.  If we were to read it literally, this is what it would say.

num must be equal to 5 AND num must be equal to ‘5’

If you refresh your HTML document, you’ll get a message saying This is true. Now, you may be thinking, why does it say this? We set our variable num to the value of 5. It’s datatype is a number and not a string. So, the first condition is true, but the 2nd condition should return false because it’s comparing a number to a string.

Well, JavaScript is very forgiving. JavaScript will temporarily convert your number to a string and check if it’s the same. This can be useful, but also harmful at the same time. Using logical operators allow you to run the same block of code under certain conditions without having to create a new conditional statement.

There’s another logical operator which is the OR operator. Let’s update our if statement to this.

An OR logical operator looks like this ||. It’s represented by 2 vertical lines. An OR logical operator works similar to the AND operator. In this case, either statement can be true. We know for sure that our variable num is equal to 5, but it isn’t equal to 7. Since, we’re using the OR operator, if  one statement is true, then our block of code is executed. In our case, the first condition is true, so it’ll run.

You can combine logical operators. Update your if statement to this.

In our conditional statement, we check if num is equal to 6 OR 5 is equal to 6 AND num is equal to 7. This may seem confusing, but basically we’re checking if one condition is true or 2 other conditions is true. Refresh your page and you should get a true message.

Conclusion

Why should you use logical operators and not just use else if statements to check for other conditions? You should use both. The reason you use logical operators is so you don’t have to have one long condition tree in your script. You don’t have to copy and paste repetitive code.

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link