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 or operator and if so, how does it make sense that a variable can have two different values?
This is an
ORoperator. What you need to understand is:Non-boolean values are converted to a boolean when used in a logic operator. Values that convert to
falseare called “falsy” and values that convert totrueare called “truthy”. Falsy values include things like0,undefined,null, and so on. See more at Truthy and Falsy: When All is Not Equal in JavaScript.The
ORoperator short-circuits: it keeps evaluating expressions until it finds on that istrue, and then stops.So,
var time = $(el).data('start') || new Date();means “settimeto thestartdata of theelelement, OR, if that’s falsy, use the current time”.