JavaScript type conversion: (true && 1) vs (true | | 1)

后端 未结 6 1801
清酒与你
清酒与你 2020-12-09 17:31

JavaScript is non-strictly typed language as Java,for example.

As we know, it converts value of result dependently upon context:

\"2\" + \"3\" r

6条回答
  •  无人及你
    2020-12-09 18:16

    To quote MDC;

    &&; Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
    ||; Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

    So in the first example, 1 is being returned because expr1 cannot be converted to false.

    In the second example, true can be converted to true, so it's returned.

提交回复
热议问题