Another widely used pattern for this is the use of the Logical OR || (a little more readable than your ternary IMHO):
This operator will return its second operand if the first evaluates to false , otherwise it will return the first.
It is safe to use it when you expect an object, since these false values ββare null , undefined , NaN , 0 , a string of zero length and, of course, false .
It is useful for me to set default values ββfor function arguments, when, of course, any of the false values ββis expected as a valid function:
function test (arg1) { arg1 = arg1 || "default value";
source share