Logical Operators with in JavaScript
Logical operators are used to make decisions based on multiple conditions. In JavaScript, there are three types of logical operators:
- Logical AND (
&&) - Logical OR (
||) - Logical NOT (
!)
Let's explore each of these operators with examples.
1. Logical AND (&&)
The logical AND operator returns true if both operands are true. Otherwise, it returns false.
Syntax:
operand1 && operand2
Example 1: Basic Usage
// Logical AND (&&)
// Returns TRUE if both operands are TRUE
console.log(true && true); // Output: true
console.log(false && true); // Output: false
If either operand is false, the result is false.
Example 2: Real-world Use Case
Let's imagine we want to build an application for approving loans. The applicant must have a high income and a good credit score to be eligible for a loan.
let highIncome = true;
let goodCreditScore = true;
let eligibleForLoan = highIncome && goodCreditScore;
console.log(eligibleForLoan); // Output: true
If either highIncome or goodCreditScore is false, eligibleForLoan will be false.
2. Logical OR (||)
The logical OR operator returns true if at least one of the operands is true. If both operands are false, it returns false.
Syntax:
operand1 || operand2
Example 1: Basic Usage
// Logical OR (||)
// Returns TRUE if one of the operands is TRUE
console.log(true || false); // Output: true
console.log(false || false); // Output: false
As long as one operand is true, the result is true.
Example 2: Real-world Use Case
Continuing with our loan application example, let's say an applicant can be eligible for a loan if they have either a high income or a good credit score.
let highIncome = false;
let goodCreditScore = true;
let eligibleForLoan = highIncome || goodCreditScore;
console.log(eligibleForLoan); // Output: true
If both highIncome and goodCreditScore are false, eligibleForLoan will be false.
3. Logical NOT (!)
The logical NOT operator inverts the value of its operand. If the operand is true, it returns false, and if the operand is false, it returns true.
Syntax:
!operand
Example: Real-world Use Case
Let's consider a scenario where we want to mark an application as refused if the applicant is not eligible for a loan.
let highIncome = false;
let goodCreditScore = false;
let eligibleForLoan = highIncome || goodCreditScore;
console.log('Eligible:', eligibleForLoan); // Output: false
// Logical NOT (!)
let applicationRefused = !eligibleForLoan;
console.log('Application Refused:', applicationRefused); // Output: true
Examples and Explanation
Logical AND (&&)
The logical AND operator returns true if both conditions are true.
let highIncome = true;
let goodCreditScore = true;
let eligibleForLoan = highIncome && goodCreditScore;
console.log(eligibleForLoan); // true
highIncome = false;
console.log(highIncome && goodCreditScore); // false
Logical OR (||)
The logical OR operator returns true if at least one condition is true.
let highIncome = false;
let goodCreditScore = true;
let eligibleForLoan = highIncome || goodCreditScore;
console.log(eligibleForLoan); // true
highIncome = false;
goodCreditScore = false;
console.log(highIncome || goodCreditScore); // false
Logical NOT (!)
The logical NOT operator inverts the boolean value.
let highIncome = false;
let goodCreditScore = false;
let eligibleForLoan = highIncome || goodCreditScore;
console.log('Eligible:', eligibleForLoan); // false
let applicationRefused = !eligibleForLoan;
console.log('Application Refused:', applicationRefused); // true
Summary
Logical operators in JavaScript are essential for making decisions based on multiple conditions:
-
Logical AND (
&&): Returnstrueif both operands aretrue.- Syntax:
operand1 && operand2 - Example:
console.log(true && true);// true
- Syntax:
-
Logical OR (
||): Returnstrueif at least one operand istrue.- Syntax:
operand1 || operand2 - Example:
console.log(true || false);// true
- Syntax:
-
Logical NOT (
!): Inverts the boolean value of its operand.- Syntax:
!operand - Example:
console.log(!true);// false
- Syntax:
Understanding these operators will help you make complex logical decisions in your code, making your programs more flexible and robust.