Вопрос по javascript – Что означают две вертикальные линии в значении объекта в javascript? [Дубликат]

17

Possible Duplicate:
What does the || operator do?

Error: User Rate Limit Exceeded||Error: User Rate Limit Exceeded

var time =  $(el).data('start') || new Date();

Error: User Rate Limit ExceededorError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Esailija
Error: User Rate Limit Exceeded tim peterson
Error: User Rate Limit Exceeded tim peterson

Ваш Ответ

5   ответов
5

$(el).data('start')timefalsenew Date()

...if $(el).data('start') is "falsy" (that is, undefined, null, 0, false, an empty string, or NaN), then new Date() is evaluated and assigned to time.

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Logical Operators.
Error: User Rate Limit Exceeded$(el).data('start')Error: User Rate Limit ExceededNaNError: User Rate Limit Exceedednew Date()Error: User Rate Limit ExceededtimeError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded tim peterson
-1

logical sum. var time = $(el).data('start') || new Date();$(el).data('start')undefinedfalsetimenew Date

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededlogical sumError: User Rate Limit Exceededor? tim peterson
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
39

OR

Non-boolean values are converted to a boolean when used in a logic operator. Values that convert to false are called "falsy" and values that convert to true are called "truthy". Falsy values include things like 0, undefined, null, and so on. See more at Truthy and Falsy: When All is Not Equal in JavaScript.

The OR operator short-circuits: it keeps evaluating expressions until it finds on that is true, and then stops.

var time = $(el).data('start') || new Date();timestartel

Error: User Rate Limit Exceeded tim peterson
Error: User Rate Limit Exceeded
5
exp1 || exp2 

a = something

if (prop)

a = prop

a = prop || something
1

if ($(el).data('start')) {
  time = $(el).data('start');
} else {
  time = new Date();
}

Похожие вопросы