title: What does the operator |= do in JavaScript?
body: <p>I found the following code in a JS project:</p>
  
  <pre><code>var a = new Array();
  a[0] = 0;
  for (var b = 0; b &lt; 10; b++) {
    a[0] |= b; 
  }
  </code></pre>
  
  <p>What does the <code>|=</code> do in the body of the for loop?</p>
  
  <p>The code example is dubious, but has been presented <a href="http://www.html5rocks.com/en/tutorials/speed/v8/#toc-topic-numbers" rel="noreferrer">here</a> by V8 for an example of improved performance.</p>
  
  <p><strong>Updated Example</strong></p>
  
  <p>The above example is equivalent to <code>var a = [15];</code> for most intents and purposes.  A more realistic example for the <code>|=</code> operator would be to set up binary flags in a single variable, for example on a permission object:</p>
  
  <pre><code>//Set up permission masks
  var PERMISSION_1_MASK = parseInt('0001',2);
  var PERMISSION_2_MASK = parseInt('0010',2);
  ..
  
  //Set up permissions
  userPermissions = 0;
  userPermissions |= hasPermissionOne &amp;&amp; PERMISSION_1_MASK;
  userPermissions |= hasPermissionTwo &amp;&amp; PERMISSION_2_MASK;
  ..
  
  //Use permissions
  if(userPermissions &amp; PERMISSION_1_MASK){
      ..//Do stuff only allowed by permission 1
  }
  </code></pre>