Understanding AND and OR Operators in JavaScript

Logical operators are essential tools in JavaScript for making decisions and controlling the flow of your code. The AND (&&) and OR (||) operators are two of the most commonly used logical operators. In this article, we’ll explore how these operators work, their differences, and how to use them effectively in your code.

What Are Logical Operators?

Logical operators are used to combine one or more conditions. They return a boolean value (true or false) based on the evaluation of the conditions. JavaScript has three logical operators:

  1. AND (&&)
  2. OR (||)
  3. NOT (!)

This article focuses on the AND and OR operators.

The AND Operator (&&)

The AND operator returns true only if both conditions it is evaluating are true. If either of the conditions is false, it returns false. The syntax for using the AND operator is as follows:

condition1 && condition2;

Example of AND Operator

Let’s see an example of how the AND operator works:

let a = 5;
let b = 10;

if (a > 3 && b > 7) {
  console.log("Both conditions are true");
} else {
  console.log("At least one condition is false");
}

In this example, both conditions a > 3 and b > 7 are true, so the output will be “Both conditions are true”.

Short-Circuit Evaluation

The AND operator also supports short-circuit evaluation. This means that if the first condition is false, the second condition is not evaluated because the result is already known to be false. This can be useful for preventing errors or unnecessary computations.

let x = 0;

if (x !== 0 && 1 / x > 2) {
  console.log("This won't run");
}

In this case, since x is 0, the first condition x !== 0 is false, so the second condition 1 / x > 2 is not evaluated, avoiding a division by zero error.

The OR Operator (||)

The OR operator returns true if at least one of the conditions it is evaluating is true. It returns false only if both conditions are false. The syntax for using the OR operator is as follows:

condition1 || condition2;

Example of OR Operator

Let’s see an example of how the OR operator works:

let c = 15;
let d = 20;

if (c > 20 || d > 15) {
  console.log("At least one condition is true");
} else {
  console.log("Both conditions are false");
}

In this example, the second condition d > 15 is true, so the output will be “At least one condition is true”.

Short-Circuit Evaluation with OR

Similar to the AND operator, the OR operator also supports short-circuit evaluation. However, in this case, if the first condition is true, the second condition is not evaluated because the result is already known to be true.

function logMessage() {
  console.log("Function called");
  return true;
}

if (true || logMessage()) {
  console.log("This runs");
}

In this example, the first condition is true, so the second condition logMessage() is not executed, and the function is not called.

Difference Between AND and OR Operators

OperatorReturns true If…
&&Both conditions are true
||At least one condition is true

Common Use Cases

Conditional Statements

Both operators are commonly used in conditional statements to control the flow of the program.

Example with AND

let age = 18;
let isAdult = true;

if (age >= 18 && isAdult) {
  console.log("You can vote");
}

Example with OR

let hasCreditCard = true;
let hasDebitCard = false;

if (hasCreditCard || hasDebitCard) {
  console.log("You can make a purchase");
}

Function Calls

Logical operators can be used to conditionally call functions.

Example with AND

function greet() {
  console.log("Hello!");
}

function welcome() {
  console.log("Welcome!");
}

let shouldGreet = true;
let shouldWelcome = true;

shouldGreet && greet();
shouldWelcome && welcome();

Example with OR

function login() {
  console.log("Logged in");
}

function signup() {
  console.log("Signed up");
}

let isUser = false;

isUser || signup();

Truthy and Falsy Values

Logical operators can also be used with truthy and falsy values. JavaScript treats certain values as true or false in a boolean context.

Example with AND

let value = 0;

value && console.log("Value is truthy");

Since 0 is a falsy value, the condition is false, and the message is not logged.

Example with OR

let value = null;

value || console.log("Value is falsy");

Since null is a falsy value, the condition is true, and the message is logged.

Order of Operations

When using both AND and OR operators in the same expression, the order of operations matters. The AND operator has higher precedence than the OR operator. This means that AND operations are evaluated before OR operations.

Example

let a = 5;
let b = 10;
let c = 15;

if (a < b || a < c && b < c) {
  console.log("Condition is true");
}

In this example, the AND operation a < c && b < c is evaluated first. Since both conditions are true, the result is true. Then, the OR operation true || true is evaluated, resulting in true, and the message is logged.

Comparison Table

OperatorDescriptionReturns true If…
&&ANDBoth conditions are true
||ORAt least one condition is true

Frequently Asked Questions

Q1: What is the difference between && and ||?

The AND operator (&&) returns true only if both conditions are true, while the OR operator (||) returns true if at least one condition is true.

Q2: Can I use && and || together in an expression?

Yes, you can use both operators together. However, be aware of the operator precedence, where && is evaluated before ||.

Q3: What is short-circuit evaluation?

Short-circuit evaluation is when the second condition is not evaluated because the result is already determined by the first condition. This can help prevent errors or optimize code.

Q4: What are truthy and falsy values?

Truthy values are values that are considered true in a boolean context, such as non-zero numbers, non-empty strings, and objects. Falsy values are values considered false, such as 0, null, undefined, and false.

Q5: How do I remember which operator has higher precedence?

Remember that AND (&&) comes before OR (||) in operator precedence, similar to how multiplication comes before addition in arithmetic operations.

Conclusion

Understanding the AND and OR operators is crucial for writing effective and efficient JavaScript code. By using these operators appropriately, you can control the flow of your program, make decisions, and avoid errors through short-circuit evaluation. Practice using these operators in different scenarios to become more comfortable with them.

Index
Scroll to Top