tim peterson tim peterson - 1 year ago 102
Javascript Question

What do two vertical lines in an object value mean in javascript?

Possible Duplicate:

What does the || operator do?

Maybe somebody can provide a better code snippet, but what does
mean in the following?:

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

Is it an
operator and if so, how does it make sense that a variable can have two different values?

Answer Source

This is an OR operator. What you need to understand is:

  • 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.

So, var time = $(el).data('start') || new Date(); means "set time to the start data of the el element, OR, if that's falsy, use the current time".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download