Attacklab.wmd_env.buttons=Attacklab.wmd_env.buttons||_4;
what does the || do in this case?
Adds _4 to the array which is Attacklab.wmd_env.buttons?
The ||
operator in JavaScript returns the value on the left if that value does not evaluate to false
, otherwise it returns the value on the right.
From Mozilla's Core JavaScript 1.5 Reference:
expr1 || expr2
Returnsexpr1
if it can be converted to true; otherwise, returnsexpr2
. Thus, when used with Boolean values,||
returns true if either operand is true; if both are false, returns false.
So, in this case, if Attacklab.wmd_env.buttons
doesn't have a value, it sets the value to _4
.
It's a fancy way of writing
if(!Attacklab.wmd_env.buttons)
Attacklab.wmd_env.buttons = _4;
It's nice for providing default values. Keep in mind that not only null
and undefined
will trigger the conditional, but also 0
, false
and ''
, ie everything which is considered false
in boolean contexts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With