Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Expressions and operators
  5. Logical AND (&&)

Logical AND (&&)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Thelogical AND (&&) (logical conjunction) operator for a set of boolean operands will betrue if and only if all the operands aretrue. Otherwise it will befalse.

More generally, the operator returns the value of the firstfalsy operand encountered when evaluating from left to right, or the value of the last operand if they are alltruthy.

Try it

const a = 3;const b = -2;console.log(a > 0 && b > 0);// Expected output: false

Syntax

js
x && y

Description

Logical AND (&&) evaluates operands from left to right, returning immediately with the value of the firstfalsy operand it encounters; if all values aretruthy, the value of the last operand is returned.

If a value can be converted totrue, the value is so-calledtruthy. If a value can be converted tofalse, the value is so-calledfalsy.

Examples of expressions that can be converted to false are:

  • false;
  • null;
  • NaN;
  • 0;
  • empty string ("" or'' or``);
  • undefined.

The AND operator preserves non-Boolean values and returns them as they are:

js
result = "" && "foo"; // result is assigned "" (empty string)result = 2 && 0; // result is assigned 0result = "foo" && 4; // result is assigned 4

Even though the&& operator can be used with non-Boolean operands, it is still considered a boolean operator since its return value can always beconverted to aboolean primitive.To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a doubleNOT operator or theBoolean constructor.

Short-circuit evaluation

The logical AND expression is a short-circuit operator.As each operand is converted to a boolean, if the result of one conversion is found to befalse, the AND operator stops and returns the original value of that falsy operand; it doesnot evaluate any of the remaining operands.

Consider the pseudocode below.

(some falsy expression) && expr

Theexpr part isnever evaluated because the first operand(some falsy expression) is evaluated asfalsy.Ifexpr is a function, the function is never called.See the example below:

js
function A() {  console.log("called A");  return false;}function B() {  console.log("called B");  return true;}console.log(A() && B());// Logs "called A" to the console due to the call for function A,// && evaluates to false (function A returns false), then false is logged to the console;// the AND operator short-circuits here and ignores function B

Operator precedence

The AND operator has a higher precedence than the OR operator, meaning the&& operator is executed before the|| operator (seeoperator precedence).

js
true || false && false; // truetrue && (false || false); // false(2 === 3) || (4 < 0) && (1 === 1); // false

Examples

Using AND

The following code shows examples of the&& (logical AND)operator.

js
a1 = true && true; // t && t returns truea2 = true && false; // t && f returns falsea3 = false && true; // f && t returns falsea4 = false && 3 === 4; // f && f returns falsea5 = "Cat" && "Dog"; // t && t returns "Dog"a6 = false && "Cat"; // f && t returns falsea7 = "Cat" && false; // t && f returns falsea8 = "" && false; // f && f returns ""a9 = false && ""; // f && f returns false

Conversion rules for booleans

Converting AND to OR

The following operation involvingbooleans:

js
bCondition1 && bCondition2

is always equal to:

js
!(!bCondition1 || !bCondition2)

Converting OR to AND

The following operation involvingbooleans:

js
bCondition1 || bCondition2

is always equal to:

js
!(!bCondition1 && !bCondition2)

Removing nested parentheses

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression provided that certain rules are followed.

The following composite operation involvingbooleans:

js
bCondition1 || (bCondition2 && bCondition3)

is always equal to:

js
bCondition1 || bCondition2 && bCondition3

Specifications

Specification
ECMAScript® 2026 Language Specification
# prod-LogicalANDExpression

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp