0

Say: arr1 = ['a', 'b', 'c'] and arr2 = [0, 1, 2]

We want an object {'a' : 0}

This function throws a syntax error:

function makeObject(arr1, arr2) {
  return {arr1[0] : arr2[0]}
}

but this is okay:

function makeObject(arr1, arr2) {
  return {[arr1[0]] : arr2[0]}
}

Why and where can I find more documentation on this behavior?

cvb0rg
  • 73
  • 6
  • Computed property names – Ele Apr 27 '18 at 02:27
  • You always have to use square brackets when defining a property name with a variable. The only time you can omit them is when the property name is going to be a plain pre-defined string. – CertainPerformance Apr 27 '18 at 02:27
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names – dave Apr 27 '18 at 02:27

0 Answers0