Why does this method for counting duplicates of an array and storing it into an object work?
<pre><code>var counts = {};
var your_array = ['a', 'a', 'b', 'c'];
your_array.forEach(function(x) { 
  counts[x] = (counts[x] || 0) + 1; 
});
console.log(your_array);
</code></pre>

<p>In javascript, why do you have to do <code>counts[x] = (counts[x] || 0) + 1;</code> Why doesn't <code>counts[x] += 1;</code> work?</p>

<p>This will output <code>{ a: 2, b: 1, c: 1}</code>, but why? </p>