Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Logical OR (||)

BaselineWidely available

Thelogical OR (||) (logical disjunction) operator for a set of operandsis true if and only if one or more of its operands is true. It is typically used withboolean (logical) values. When it is, it returns a Boolean value. However,the|| operator actually returns the value of one of the specifiedoperands, so if this operator is used with non-Boolean values, it will return anon-Boolean value.

Try it

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

Syntax

js
x || y

Description

Ifx can be converted totrue, returnsx; else, returnsy.

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

Examples of expressions that can be converted to false are:

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

Even though the|| operator can be used with operands that are not Booleanvalues, it can still be considered a boolean operator since its return value can alwaysbe converted to aboolean primitive.To explicitly convert its return value (or any expression in general) to thecorresponding boolean value, use a doubleNOT operator or theBoolean()constructor.

Short-circuit evaluation

The logical OR expression is evaluated left to right, it is tested for possible"short-circuit" evaluation using the following rule:

(some truthy expression) || expr is short-circuit evaluated tothe truthy expression.

Short circuit means that theexpr part above isnotevaluated, hence any side effects of doing so do not take effect (e.g., ifexpr is a function call, the calling never takes place). Thishappens because the value of the operator is already determined after the evaluation ofthe first operand. See example:

js
function A() {  console.log("called A");  return false;}function B() {  console.log("called B");  return true;}console.log(B() || A());// Logs "called B" due to the function call,// then logs true (which is the resulting value of the operator)

Operator precedence

The following expressions might seem equivalent, but they are not, because the&& operator is executed before the|| operator(seeoperator precedence).

js
true || false && false; // returns true, because && is executed first(true || false) && false; // returns false, because grouping has the highest precedence

Examples

Using OR

The following code shows examples of the|| (logical OR) operator.

js
true || true; // t || t returns truefalse || true; // f || t returns truetrue || false; // t || f returns truefalse || 3 === 4; // f || f returns false"Cat" || "Dog"; // t || t returns "Cat"false || "Cat"; // f || t returns "Cat""Cat" || false; // t || f returns "Cat""" || false; // f || f returns falsefalse || ""; // f || f returns ""false || varObject; // f || object returns varObject

Note:If you use this operator to provide a default value to somevariable, be aware that anyfalsy value will not be used. If you only need tofilter outnull orundefined, consider usingthe nullish coalescing operator.

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 removeparentheses from a complex expression following some rules.

The following composite operation involvingbooleans:

js
bCondition1 && (bCondition2 || bCondition3)

is always equal to:

js
!(!bCondition1 || !bCondition2 && !bCondition3)

Specifications

Specification
ECMAScript® 2026 Language Specification
# prod-LogicalORExpression

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp