title: How does variable assignment work in JavaScript?
body: <p>So I was playing around the other day just to see exactly how mass assignment works in JavaScript.</p>
  
  <p>First I tried this example in the console:</p>
  
  <pre><code>a = b = {};
  a.foo = 'bar';
  console.log(b.foo);
  </code></pre>
  
  <p>The result was "bar" being displayed in an alert. That is fair enough, <code>a</code> and <code>b</code> are really just aliases to the same object. Then I thought, how could I make this example simpler.</p>
  
  <pre><code>a = b = 'foo';
  a = 'bar';
  console.log(b);
  </code></pre>
  
  <p>That is pretty much the same thing, isn't it? Well this time, it returns <code>foo</code> not <code>bar</code> as I would expect from the behaviour of the first example.</p>
  
  <p>Why does this happen?</p>
  
  <p><strong>N.B.</strong> This example could be simplified even more with the following code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>a = {};
  b = a;
  a.foo = 'bar';
  console.log(b.foo);
  
  a = 'foo';
  b = a;
  a = 'bar';
  console.log(b);</code></pre>
  </div>
  </div>
  </p>
  
  <p>(I suspect that JavaScript treats primitives such as strings and integers differently to hashes. Hashes return a pointer while "core" primitives return a copy of themselves)</p>