Logical Operators AND, OR, NOT in JavaScript
Exploring logical operators (&&, ||, !) and their application in JS
Logical operators are a crucial aspect of any programming language. In JavaScript, three logical operators are used: AND (&&), OR (||), and NOT (!).
These operators are used to combine and evaluate multiple conditions and determine the logical outcome of those conditions.
Let us explore these operators in more detail and their applications in JavaScript.
AND (&&) Operator
The AND operator is used to check if all conditions in a series of conditions are true. If all the conditions are true, the result of the operation is true; otherwise, the result is false. In JavaScript, the syntax for the AND operator is as follows:
(condition1) && (condition2)
For example, suppose we have two variables, personX and personY. We want to check if both the person having age greater than 5.
We can use the AND operator as follows:
if (personX > 5 && personY > 5) {
// Do something
}
In the above code, the if statement checks if both personX and personY are greater than 5. After that, if both conditions are true, the code inside the curly braces will execute.
OR (||) Operator
The OR operator is used to check if at least one condition in a series of conditions is true.
If at least one condition is true, the result of the operation is true; otherwise, the result is false.
In JavaScript, the syntax for the OR operator is as follows:
(condition1) || (condition2)
For example, suppose we have two variables, a and b. We want to check if at least one of the variables is greater than 10. We can use the OR operator as follows:
if (a > 10 || b > 10) {
// Do something
}
In the above code, the if statement checks if either a or b is greater than 10. If at least one condition is true, the code inside the curly braces will execute.
NOT (!) Operator
The NOT operator is used to negate a condition. If a condition is true, the NOT operator will make it false; if a condition is false, the NOT operator will make it true.
In JavaScript, the syntax for the NOT operator is as follows:
!condition
For example, suppose we have a variable 's' that is not equal to 5. We want to check if 's' is not equal to 5. We can use the NOT operator as follows:
if (!(s === 5)) {
// Do something
}
In the above code, the if statement checks if 's' is not equal to 5. A little confusion might happen in this one.
If the code doesn't have (!) before (s === 5), then it will check if 's' is equal to 5, but it has NOT operator before and therefore it will check for viceversa and then runs the condition inside curly braces.
The NOT operator negates the equality check, making the condition true if z is not equal to 5
Combining Logical Operators
In JavaScript, logical operators can be combined to create complex conditions that evaluate multiple expressions.
The combination of logical operators allows developers to create more sophisticated programs with intricate logical conditions.
By combining logical operators, developers can create conditions that evaluate multiple expressions with varying degrees of complexity.
See this example below: (can be written in .js file)
let age = 25;
let income = 50000;
let isStudent = true;
if ((age > 18 && income > 25000) || isStudent) {
console.log("You qualify for a discount!");
} else {
console.log("Sorry, you do not qualify for a discount.");
}
In this code, we have three variables:
- age
- income
- isStudent.
We want to check if a user qualifies for a discount based on their age, income, and student status.
We use the AND operator to check if the user is over 18 and has an income greater than 25,000.
If this condition is true, we combine it with the OR operator to check if the user is a student.
If either of these conditions is true, we print a message saying the user qualifies for a discount. Otherwise, we print a message saying they do not qualify.
This code demonstrates how logical operators can be combined to create more complex conditions in JavaScript. By using a combination of logical operators, we can create programs that are capable of handling a wide range of logical conditions and making more informed decisions based on those conditions.
Combining logical operators is an important skill in JS. By mastering this skill, one can create more robust and efficient programs that are capable of handling complex logical conditions.
Find more information about logical operators here.
Stay tuned for more informative stories. Feel free to comment any additional information you have.
Show your support, buy me a book here.
Connect with me on Instagram for more such content.
About the Creator
Prashant Patel
Canada based Civil Engineer(Structural Detailer), Actively learning Programming/Coding from Free Resources across internet.
Happy to share my coding journey with you all, Freelance Writer📝 | Content Creator | Cricket Enthusiast🏏.

Comments
There are no comments for this story
Be the first to respond and start the conversation.