Javascript AND operator within assignment

后端 未结 6 694
滥情空心
滥情空心 2020-11-22 05:20

I know that in JavaScript you can do:

var oneOrTheOther = someOtherVar || \"these are not the droids you are looking for...\";

where the va

6条回答
  •  旧巷少年郎
    2020-11-22 05:47

    Basically, the Logical AND operator (&&), will return the value of the second operand if the first is truthy, and it will return the value of the first operand if it is by itself falsy, for example:

    true && "foo"; // "foo"
    NaN && "anything"; // NaN
    0 && "anything";   // 0
    

    Note that falsy values are those that coerce to false when used in boolean context, they are null, undefined, 0, NaN, an empty string, and of course false, anything else coerces to true.

提交回复
热议问题