How does variable assignment work in JavaScript?
<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';
alert(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';
alert(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>

<pre><code>a = {};
b = a;
a.foo = 'bar';
alert(b.foo);

a = 'foo';
b = a;
a = 'bar';
alert(b);
</code></pre>

<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>
