&& is guard operator aka "logical and", and || is default operator "aka logical or", we normally see the code like the following
if ( condtion1 && condition2) { }
if (condition1 || condition2) { }
&& means, If first operand is truthy, the result is second operand, else result is is first operand. It can be used to avoid null reference.
if (a){
return a.memeber;
} else {
return a;
}
//this is the same
return a && a.member
|| means, if first operand is truethy, then result is first operand, else result is second operand. It can be used to fill in default value like the following
var last = input || {}; //{} is default value
No comments:
Post a Comment