Possible Duplicate:
Javascript Shorthand – What Does the '||' Operator Mean When Used in an Assignment?
var variable = obj1 || obj2;
Does it mean this?
var variable;
if (obj1)
{
variable = obj1;
}
else if (obj2)
{
variable = obj2:
}
Is it considered bad practise?
The
||operator returns its left hand side if resolves to be a true value, otherwise it returns its right hand side.So it means the same as:
Note else, not, else if.
It is a common pattern and not usually considered bad practise.
The gotcha is that you need to be sure you want
if (obj)and notif (typeof obj !== "undefined").