And Or in JavaScript - ECMAScript 2021

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: JavaScript - ECMAScript 2021

In JavaScript (ECMAScript 2021), and and or are logical operators that can be used to create conditional expressions.

The symbol && represents the and operator, while the symbol || represents the or operator.

The and operator && returns true if both operands are true, otherwise it returns false.

Example:

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

// Using && (and) operator
if (a > b && b < c) {
console.log("a is greater than b AND b is less than c");
}

In this example, the code inside the if statement will be executed if a is greater than b AND b is less than c.

The or operator || returns true if at least one operand is true, otherwise it returns false.

Example:

const isFemale = true;
const isTall = false;

// Using || (or) operator
if (isFemale || isTall) {
console.log("You are either female or tall or both");
}

In this example, the code inside the if statement will be executed if the person is either female OR tall OR both.

Note that both && and || operators short-circuit, which means that they evaluate the second operand only if necessary. If the first operand is enough to determine the result, the second operand is not evaluated at all.

For example, in the expression a || b, if a is true, b is not evaluated, because the result is already true. Similarly, in the expression a && b, if a is false, b is not evaluated, because the result is already false. This can be useful for performance reasons and for avoiding errors when evaluating complex expressions.