Behaviour of Javascript array objects
<pre><code>function map(f, a) {
  for(var i in a) {
    a[i] = f(a[i]);
  }
}

var a = [0, 1, 2, 3];
map(function(x) { return x = x * x }, a);
console.log(a);
</code></pre>

<p>a: <code>[0, 1, 4, 9]</code></p>

<p>However, If I change <code>map(f, a)</code> to:     </p>

<pre><code>function map(f, a) {
  for(var i in a) {
    f(a[i]);
  }
}
var a = [0, 1, 2, 3];
map(function(x) { return x = x * x }, a);
console.log(a);
</code></pre>

<p>a remains unchanged as: <code>[0, 1, 2, 3]</code></p>

<p>I'm not sure what's happening here. It appears as if the interpreter considers <code>a[i]</code> as a reference to a property of the object <code>a</code> in <code>map(f, a)</code> but once passed into <code>f</code> it turns into a <code>typeof number</code>.   </p>
