I saw this construction in order to get the browser viewport width:
function () { return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; }
I understand the browser quirks involved. What I don’t understand is why || returns the value. So I tried this alert(undefined || 0 || 3); and sure enough, it alerts 3. I find this bizarre, because I expect true or false. Could anyone explain what’s going on?
Take a look at the ECMAScript standards section 11.11 Binary Logical Operators
So it evaluates the boolean conversion of each operand, but returns the actual value of the operand.
If you want to know how Javascript converts values to a boolean, see section 9.2 ToBoolean