Possible Duplicate:
What does “options = options || {}” mean in Javascript?
I have seen this in JS:
item = item || {};
I'm guessing it's some variation of a ternary operator but what does is actually do?
Possible Duplicate:
What does “options = options || {}” mean in Javascript?
I have seen this in JS:
item = item || {};
I'm guessing it's some variation of a ternary operator but what does is actually do?
(expr1 || expr2)
"Returns expr1 if it can be converted to true; otherwise, returns expr2."
So when expr1
is (or evaluates to) one of these 0,"",false,null,undefined,NaN
, then expr2
is returned, otherwise expr1
is returned
It's called redundancy, but in this case it's a good thing. Basically, if item
is not defined (or otherwise falsy (false
, 0
, ""
...), then we give it a default value.
Most common example is in events:
evt = evt || window.event;
If item exists, set item to item, or set it to {}
It equates to:
if( !item ){ item = {}; }