W3docs

JavaScript Logical Operators (&&, ||, !)

Logical operators are pivotal for controlling the flow and decision-making in JavaScript. This guide is crafted to help beginners understand and effectively use

Logical operators are pivotal for controlling the flow and decision-making in JavaScript. This guide explains how to use JavaScript's logical operators—&&, ||, !, the related ??, and the !! idiom—and, just as importantly, the two behaviors that trip most people up: short-circuit evaluation and value-returning semantics. Every example below is runnable.

Overview of JavaScript Logical Operators

Logical operators evaluate one or more operands and return a value. They are a core part of JavaScript's operators and are most often used inside conditions, but they do far more than produce true/false:

  • && (Logical AND)
  • || (Logical OR)
  • ?? (Nullish Coalescing Operator — technically distinct, but solves the same "pick a value" problem)
  • ! (Logical NOT)
  • !! (Double NOT — the convert-to-Boolean idiom)

Truthy and falsy values

Before the operators make sense, you need to know what JavaScript treats as "true" and "false" in a Boolean context. There are exactly eight falsy values; everything else is truthy.

javascript— editable

This matters because &&, ||, and ! test operands for truthiness, not strictly for the Boolean true. See JavaScript Data Types for the underlying value types.

Short-circuit evaluation and value-returning

Two rules govern almost everything these operators do:

  1. Short-circuit: evaluation stops as soon as the result is known. The right-hand operand may never run.
  2. They return an operand, not a Boolean: && and || give back one of the original values, not true/false.
javascript— editable

This is exactly why || works as a default-value picker and why && works as a guard, as shown later.

Logical AND (&&)

In a Boolean context, && is true only when both operands are truthy. More precisely, it returns the first falsy operand, or the last operand if they are all truthy. Use it when several conditions must all hold, or as a guard that runs the right side only when the left side is truthy.

javascript— editable

In this example, both isLoggedin and hasPermissions must be truthy for access to be granted. If either is falsy, access is denied — and because of short-circuiting, if isLoggedin is false, hasPermissions is never even evaluated.

Logical OR (||)

In a Boolean context, || is true when at least one operand is truthy. More precisely, it returns the first truthy operand, or the last operand if they are all falsy. This is what makes it a fallback / default-value operator.

javascript— editable

Here, the operation can continue if either networkStatus is 'good' or savedOffline is true, providing a fallback if the network status is poor.

Nullish Coalescing Operator (??)

The ?? operator returns its right-hand side only when the left-hand side is null or undefined (collectively called nullish). It ignores other falsy values like 0, '', and false. That single difference is what separates it from ||.

javascript— editable

Here itemCount is 0. With ??, defaultCount stays 0 because 0 is not nullish — so a legitimate zero is preserved.

?? vs ||

|| falls back on any falsy value; ?? falls back only on null/undefined. When 0, '', or false are valid inputs, that distinction is a real bug-prevention measure.

javascript— editable

Reach for ?? when you only want a default for "no value at all," and keep || when any falsy value should trigger the fallback.

Warning

You cannot mix ?? with && or || without parentheses — a || b ?? c is a SyntaxError. Write (a || b) ?? c to make the grouping explicit.

Logical NOT (!)

The ! operator inverts the truthiness of its operand and always returns a Boolean. It is useful for toggling states or checking for non-conditions.

javascript— editable

This example checks if isActive is false, and if so, it indicates that activation is required.

Double NOT (!!)

Applying ! twice converts any value to its Boolean equivalent: the first ! inverts (and coerces to Boolean), the second flips it back. The result strictly represents either true or false.

javascript— editable

!!0 evaluates to false because 0 is a falsy value in JavaScript. The double NOT clarifies the Boolean conversion.

Using !! for Object Property Boolean Status Check

Sometimes, you want to check if an object property is not only present but also truthy. The !! operator is perfect for converting any value to a strictly Boolean context, making it clear and explicit whether the property meets the conditions.

Consider a scenario where you have a user object that might have a property called isActive. You want to perform an action only if isActive is not just present but truthy.

javascript— editable

In this example:

  • The user object has a property isActive which is undefined.
  • Using !!user.isActive converts undefined to false.
  • Since isActive is undefined (which is a falsy value), !!undefined results in false, and the output will be "John is not active."

Practical Usage Scenarios

Conditional Rendering

javascript— editable

This snippet demonstrates the use of && for conditional rendering, showing 'UserDashboard' only when isLoggedIn is true.

Managing Defaults with Logical OR

javascript— editable

If currentUser is null, userName defaults to 'Guest'. This ensures that a greeting is always personalized.

Simplifying Complex Conditions

javascript— editable

This code grants full access only to verified users who are either admins or editors, combining both && and ||. The parentheses are important — see operator precedence below.

Operator Precedence

When operators are mixed, JavaScript evaluates them in a fixed order. From highest to lowest precedence among these:

  1. ! (Logical NOT) — highest
  2. && (Logical AND)
  3. || (Logical OR) and ?? (these have the same precedence, which is why they cannot be mixed without parentheses)

Comparison operators (<, ===, etc.) bind tighter than && and ||, so they are evaluated first. See Comparison Operators for those.

javascript— editable

When in doubt, add parentheses. They cost nothing and make intent unambiguous.

Best Practices

  • Avoid Complexity: Keep logical expressions simple. If they become too complex, break them into smaller, manageable parts.
  • Leverage Short-Circuiting: Utilize the short-circuit nature of && and || to optimize performance by avoiding unnecessary evaluations.
  • Explicit Boolean Conversion: Use !! to clearly convert values to true or false, enhancing readability and predictability of your code.
  • Consistent Comparisons: Ensure consistent data types in comparisons to prevent unexpected behavior due to JavaScript's type coercion.

Conclusion

Logical operators are essential for writing effective JavaScript. Remember the two ideas that explain almost everything they do: short-circuit evaluation (the right operand may never run) and value-returning (&& and || return one of the operands, not a Boolean). Use ?? instead of || whenever 0, '', or false are valid values, and add parentheses whenever precedence is unclear.

To go further, see the Conditional (Ternary) Operator for choosing between two values inline, and Comparison Operators for building the conditions that feed into logical expressions.

Practice

Practice
What is the behavior of the logical operators in JavaScript?
What is the behavior of the logical operators in JavaScript?
Was this page helpful?