Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>||</code> is the <em>logical-or</em> operator. See <strong><a href="http://msdn.microsoft.com/en-us/library/aa691310(VS.71).aspx" rel="noreferrer">here</a></strong>. It evaluates to <code>true</code> if at least one of the operands is true. You can only use it with boolean operands; it is an error to use it with integer operands.</p> <pre><code>// Example var one = true || bar(); // result is true; bar() is never called var two = true | bar(); // result is true; bar() is always called </code></pre> <p><code>|</code> is the <em>or</em> operator. See <strong><a href="http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx" rel="noreferrer">here</a></strong>. If applied to boolean types, it evaluates to <code>true</code> if at least one of the operands is true. If applied to integer types, it evaluates to another number. This number has each of its bits set to 1 if at least one of the operands has a corresponding bit set.</p> <pre><code>// Example var a = 0x10; var b = 0x01; var c = a | b; // 0x11 == 17 var d = a || b; // Compile error; can't apply || to integers var e = 0x11 == c; // True </code></pre> <p>For boolean operands, <code>a || b</code> is <em>identical</em> to <code>a | b</code>, with the single exception that <code>b</code> is not evaluated if <code>a</code> is true. For this reason, <code>||</code> is said to be "short-circuiting".</p> <blockquote> <p>If the difference a given piece of code has between them is irrelevant, which should I default to as a best-practise?</p> </blockquote> <p>As noted, the difference isn't irrelevant, so this question is partially moot. As for a "best practice", there isn't one: you simply use whichever operator is the correct one to use. In general, people favor <code>||</code> over <code>|</code> for boolean operands since you can be sure it won't produce unnecessary side effects.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload