Logical Operators

Introduction

Logical Operators are used to combine or evaluate multiple conditions in JavaScript. They return a Boolean value (true or false) based on the logical relationship between the conditions.

Logical operators are commonly used in if statements, loops, form validation, authentication, and decision-making. They allow developers to create more complex conditions by combining multiple comparisons.

For automation engineers, logical operators are essential for validating multiple conditions in UI tests, API responses, database validations, and writing assertions.


What are Logical Operators?

Logical Operators are operators that combine or negate Boolean expressions.

Example:

let age = 25;
let hasLicense = true;

console.log(age >= 18 && hasLicense);

Output

true

Why Do We Need Logical Operators?

Logical operators help us:

  • Combine multiple conditions.

  • Make complex decisions.

  • Validate multiple inputs.

  • Control program flow.

  • Write better automation test assertions.

  • Improve code readability.


Types of Logical Operators

Operator Name Description
&& Logical AND Returns true only if all conditions are true
` `
! Logical NOT Reverses the Boolean value

Logical AND (&&)

The Logical AND (&&) operator returns true only if both conditions are true.

Syntax

condition1 && condition2

Example

let age = 25;
let hasLicense = true;

console.log(age >= 18 && hasLicense);

Output

true

Another Example

console.log(10 > 5 && 20 > 15);

Output

true

If either condition is false:

console.log(10 > 5 && 20 < 15);

Output

false

Logical OR (||)

The Logical OR (||) operator returns true if at least one condition is true.

Syntax

condition1 || condition2

Example

let isAdmin = false;
let isManager = true;

console.log(isAdmin || isManager);

Output

true

Another Example

console.log(10 < 5 || 20 > 15);

Output

true

If both conditions are false:

console.log(10 < 5 || 20 < 15);

Output

false

Logical NOT (!)

The Logical NOT (!) operator reverses a Boolean value.

  • true becomes false

  • false becomes true

Syntax

!condition

Example

let isLoggedIn = true;

console.log(!isLoggedIn);

Output

false

Another example:

console.log(!(10 > 5));

Output

false

Truth Table for Logical Operators

Logical AND (&&)

Condition 1 Condition 2 Result
true true true
true false false
false true false
false false false

Logical OR (||)

Condition 1 Condition 2 Result
true true true
true false true
false true true
false false false

Logical NOT (!)

Value Result
true false
false true

Combining Multiple Conditions

Logical operators can combine more than two conditions.

let age = 25;
let hasLicense = true;
let hasInsurance = true;

console.log(age >= 18 && hasLicense && hasInsurance);

Output

true

Using Parentheses

Parentheses improve readability and control the order of evaluation.

let age = 17;
let hasPermission = true;

console.log((age >= 18) || hasPermission);

Output

true

Short-Circuit Evaluation

JavaScript uses short-circuit evaluation with logical operators.

Logical AND

If the first condition is false, JavaScript does not evaluate the second condition.

console.log(false && true);

Output

false

Logical OR

If the first condition is true, JavaScript does not evaluate the second condition.

console.log(true || false);

Output

true

Real-World Example

Suppose a website allows access only if the user is logged in and has a premium subscription.

let isLoggedIn = true;
let isPremiumUser = true;

console.log(isLoggedIn && isPremiumUser);

Output

true

Another example:

let hasCoupon = false;
let isFestivalOffer = true;

console.log(hasCoupon || isFestivalOffer);

Output

true

Automation Testing Example

Automation engineers frequently validate multiple conditions before proceeding with a test.

const statusCode = 200;
const responseTime = 450;

console.log(statusCode === 200 && responseTime < 1000);

Output

true

This verifies that:

  • The API returned a successful response.

  • The response time is within the acceptable limit.


Another example:

const title = "Dashboard";
const url = "https://example.com/dashboard";

console.log(
    title === "Dashboard" &&
    url.includes("dashboard")
);

Output

true

Example using OR:

const role = "Manager";

console.log(
    role === "Admin" ||
    role === "Manager"
);

Output

true

Common Mistakes

Confusing && with ||

Incorrect logic:

let age = 17;

console.log(age >= 18 || age <= 60);

This expression returns true for almost every age.

Correct:

console.log(age >= 18 && age <= 60);

Forgetting Parentheses

Incorrect:

let result = true || false && false;

Because && has higher precedence than ||, JavaScript evaluates:

true || (false && false)

Use parentheses to make the logic clearer.


Using Assignment Instead of Comparison

Incorrect:

if (isLoggedIn = true) {

    console.log("Welcome");

}

Correct:

if (isLoggedIn === true) {

    console.log("Welcome");

}

Or simply:

if (isLoggedIn) {

    console.log("Welcome");

}

Best Practices

Keep Conditions Simple

Break complex conditions into smaller variables when possible.

Example:

const isAdult = age >= 18;
const canDrive = hasLicense;

if (isAdult && canDrive) {

    console.log("Eligible");

}

Use Parentheses

Even when optional, parentheses improve readability.


Use Meaningful Variable Names

Good example:

const isUserLoggedIn = true;
const hasValidSubscription = true;

Instead of:

const a = true;
const b = true;

Validate Multiple Conditions Carefully

In automation testing, ensure that every required condition is included before making assertions.


Conclusion

Logical operators are essential for controlling the flow of JavaScript programs. They allow developers to combine multiple conditions, make decisions, and validate complex scenarios.

Understanding how &&, ||, and ! work helps you write cleaner, more reliable JavaScript code. These operators are heavily used in automation testing to validate API responses, UI elements, authentication, permissions, and business rules.


Frequently Asked Questions (FAQs)

What are Logical Operators in JavaScript?

Logical operators combine or evaluate Boolean expressions and return true or false.


What are the three logical operators?

  • && (Logical AND)

  • || (Logical OR)

  • ! (Logical NOT)


When does the AND (&&) operator return true?

Only when all conditions are true.


When does the OR (||) operator return true?

When at least one condition is true.


What does the NOT (!) operator do?

It reverses a Boolean value.

Example:

console.log(!true);

Output:

false

Why are logical operators important in automation testing?

Automation engineers use logical operators to combine multiple validations, verify API responses, check UI states, validate permissions, and write reliable test assertions.


Key Takeaways

  • Logical operators combine or negate Boolean expressions.

  • JavaScript provides three logical operators: &&, ||, and !.

  • && returns true only when all conditions are true.

  • || returns true if at least one condition is true.

  • ! reverses a Boolean value.

  • JavaScript uses short-circuit evaluation with logical operators.

  • Parentheses improve readability and control evaluation order.

  • Logical operators are widely used in conditions, loops, and validations.

  • They are essential for writing automation tests and application logic.

  • Understanding logical operators helps create clean, efficient, and reliable JavaScript programs.