title: JavaScript object declaration syntax - variable names as properties
body: <p>JavaScript gives you a lot of ways to declare objects. When you have most of the data available at hand, the most convenient (in my opinion) is as follows:</p>
  
  <pre><code>var person = {
      name: 'John',
      age: 23
  }; // "object literal syntax"
  </code></pre>
  
  <p>A curious thing about this syntax is that it is identical to this:</p>
  
  <pre><code>var person = {
      'name': 'John',
      'age': 23
  }; // "object literal syntax"
  </code></pre>
  
  <p>That is, you can use quotes or omit them for the property names.</p>
  
  <p>When comparing that to the way setting a single property works, you have two options:</p>
  
  <pre><code>person.birthday = "January 12"; // "dot syntax"
  </code></pre>
  
  <p>or</p>
  
  <pre><code>person['birthday'] = "January 12"; // "array syntax"
  </code></pre>
  
  <p>The "dot syntax" only works when the right operand is the actual property name. If you want to use a variable for the property name, you have to use "array syntax", i.e.:</p>
  
  <pre><code>var prop = "birthday";
  person[prop] = "January 12";
  </code></pre>
  
  <p>Now, is it possible to use a variable for the property name in the "object literal syntax"? Since it doesn't matter if you quote the property names, there doesn't seem to be an obvious way to use a variable there. I'm looking for something like this:</p>
  
  <pre><code>var prop = "birthday";
  var person = {
      name: 'John',
      age: 23,
      (prop): 'January 12'
  };
  </code></pre>
  
  <p>Here I'm using (prop) as the imaginary syntax used to express that this is a variable and not a literal string.</p>
  
  <p>Thanks.</p>