Sometimes I see in javascript code something like this:
var myObj = myObj || {};
So, what actually happen here? I suppose || operator returns true or false, but it's not correct.
The ||
operator returns the left operand if it evaluates as true, otherwise it evaluates and returns the right operand. In other words, a || b
is equivalent to a ? a : b
except that a
is only evaluated once.
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