Logical Operators in JavaScript: A Beginner’s Guide
Table of Contents
- Introduction
- Understanding Logical Operators
- AND Operator (&&)
- OR Operator (||)
- NOT Operator (!)
- Example: Password Validation
- Code Explanation
- Conclusion
1. Introduction
Logical operators are essential tools in programming, enabling developers to create robust and efficient decision-making processes.
In JavaScript, logical operators like AND, OR, and NOT are foundational for evaluating multiple conditions.
Why Learn Logical Operators?
- Combine multiple conditions in a single evaluation.
- Create efficient and maintainable code.
- Strengthen your understanding of JavaScript logic.
2. Understanding Logical Operators
AND Operator (&&)
The AND operator returns true if all conditions are met. It’s ideal for strict condition checks.
1 2 |
Syntax: condition1 && condition2 |
1 2 3 4 |
Example: if (age >= 18 && age <= 60) { console.log("Eligible for the program."); } |
OR Operator (||)
The OR operator returns true if any condition is satisfied. Use it for flexible checks.
1 2 |
Syntax: condition1 || condition2 |
1 2 3 4 |
Example: if (userType === 'admin' || userType === 'moderator') { console.log("Access granted."); } |
NOT Operator (!)
The NOT operator inverts a condition’s value. Use it to check for negations.
1 2 |
Syntax: !condition |
1 2 3 4 |
Example: if (!isLoggedIn) { console.log("Please log in first."); } |
3. Example: Password Validation
We’ll validate a password using logical operators. Rules:
- Password must be between 8 and 20 characters.
- Password must not include the ‘@’ character.
1 2 3 4 5 6 7 8 9 10 11 12 |
const password = "Pass"; // AND -> && OR -> || if ((password.length >= 8 && password.length <= 20) && !password.includes('@')) { console.log("Password is valid"); } else if (password.includes('@')) { console.log("Password contains special characters"); } else if (password.length 20) { console.log("Password is not of valid length"); } else { console.log("Password is invalid"); } |
4. Code Explanation
Let’s break down the code:
Step 1: Input Validation
1 2 |
Description: const password = "Pass"; |
Step 2: Logical AND (&&) and NOT (!)
1 2 3 |
if ((password.length >= 8 && password.length <= 20) && !password.includes('@')) { console.log("Password is valid"); } |
Step 3: Logical OR (||)
1 2 3 |
} else if (password.length 20) { console.log("Password is not of valid length"); } |
Step 4: Special Character Check
1 2 3 |
} else if (password.includes('@')) { console.log("Password contains special characters"); } |
Step 5: Default Case
1 2 3 |
} else { console.log("Password is invalid"); } |
Output:
1 2 |
For the input "Pass": Password is not of valid length |
5. Conclusion
Logical operators in JavaScript are versatile tools for evaluating multiple conditions and improving code efficiency.
Understanding their syntax and application will help you write more robust and maintainable JavaScript programs.
- Logical operators simplify conditional checks.
- AND, OR, and NOT have unique use cases.
- Practical examples enhance understanding.
For more resources, check out our JavaScript Basics and
JavaScript Conditional Statements guides.