||
returns a boolean only when both of its operands are booleans. Otherwise, it returns (from left to right) the first operand that isn't "falsy" or the last one if it can't find any.
In that function, this.b
is meant to be a number but the b
argument to the function is optional (so it could be undefined
). To make this.b
always a number, they used that. So if you didn't pass b
, it'd look like:
this.b = undefined || 0
And this.b
will be set to zero because undefined
is always "falsy".
A resource on truthy vs falsy: http://www.sitepoint.com/javascript-truthy-falsy/
The logic behind logical operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators