You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何0与布尔值进行&&运算返回0而非布尔值?

Why does 0 return 0 instead of a boolean when used with && and boolean values?

Great question! This behavior boils down to two key concepts: short-circuit evaluation for the logical AND (&&) operator, and operator precedence. Let’s break down your examples one by one to clear up the confusion.

Core Rule of the && Operator

In languages like JavaScript (and many others), the && operator doesn’t always return a boolean. Instead, it follows this logic:

  • It evaluates operands from left to right.
  • As soon as it hits a falsy value (0, false, "", null, undefined, NaN), it returns that falsy value immediately—no need to check the right operand.
  • If all operands are truthy (anything not falsy), it returns the last truthy operand.

This is called short-circuit evaluation: it stops calculating as soon as the final result is determined.

Let’s Walk Through Your Examples

First, remember that comparison operators like > have higher precedence than &&, so they get evaluated first.

1. 0 && false > 0

  • Step 1: Evaluate false > 0. When booleans are compared to numbers, they convert to numeric values: false becomes 0, so 0 > 0 results in false.
  • Step 2: The expression simplifies to 0 && false. Since 0 is a falsy value, && returns 0 right away.

2. 0 && true > 0

  • Step 1: Evaluate true > 0. true converts to 1, so 1 > 0 results in true.
  • Step 2: The expression becomes 0 && true. 0 is falsy, so the operator returns 0.

3. false && 0

  • Here, false is the left operand and it’s falsy. && returns false immediately—we never even evaluate the 0 on the right.

4. true && 0

Quick correction here: this should return 0 (not true as you mentioned). Here’s why:

  • true is truthy, so && moves to evaluate the right operand 0.
  • 0 is falsy, so the operator returns 0. If you saw it return true, that might be a typo or confusion with the || operator (which returns the first truthy value, like true || 0 → true).

5. false && 0 > false

  • Step 1: Evaluate 0 > false. false converts to 0, so 0 > 0 results in false.
  • Step 2: The expression simplifies to false && false. false is falsy, so the operator returns false.

Clearing Up Your Original Misconception

You assumed that numeric and boolean expressions with && always return a boolean, but that’s not the case. The && operator returns the actual operand value (falsy or truthy), not a converted boolean. It only acts like a boolean when used in a boolean context (like an if statement), where the return value gets coerced to a boolean automatically.

For example:

  • 1 && false returns false (a boolean) because 1 is truthy, so it returns the right operand false.
  • 1 && false > false works out to 1 && (false > false) → 1 && false → false.

内容的提问来源于stack exchange,提问作者Radha Satam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:53:58