title: Why does JavaScript convert an array of one string to a string, when used as an object key?
body: <p>I ran into a scenario where JavaScript behaves in a way that is somewhat baffling to me.</p>
  
  <p>Let's say we have an object with two keys foo &amp; bar.</p>
  
  <pre><code>a = { foo: 1, bar: 2 }
  </code></pre>
  
  <p>Then, I have an array of strings, in this case one 'foo'</p>
  
  <pre><code>b = ['foo']
  </code></pre>
  
  <p>I would expect the following:</p>
  
  <pre><code>a[b] == undefined
  a[b[0]] == 1
  </code></pre>
  
  <p>BUT, this is what happens:</p>
  
  <pre><code>a[b] == 1
  a[b[0]] == 1
  </code></pre>
  
  <p>Why does JavaScript convert <code>['foo'] -&gt; 'foo'</code> when used as a key?</p>
  
  <p>Does anyone out there know the reason?  </p>
  
  <p>How can this be prevented?</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>let a = { foo: 1, bar: 2 }
  let b = ['foo']
  
  console.log(a[b] == 1)    // expected a[b] to be undefined
  console.log(a[b[0]] == 1)  // expected a[b] to be 1</code></pre>
  </div>
  </div>
  </p>