| | 的用法

When used with Boolean operands, the || operator performs the Boolean OR operation on the two values: it returns true if either the first operand or the second operand is true, or if both are true. If both operands are false, it returns false.

    All know the above usage, but I mention the following usage:

An idiomatic usage of this operator is to select the first value in a set of alternatives that is defined and non-null (that is, the first value that does not convert to false). Here is an example:

// If max_width is defined, use that.  Otherwise look for a value in
// the preferences object.  If that is not defined use a hard-coded constant.
var max = max_width || preferences.max_width || 500;

你可能感兴趣的:(javascript ||)