1

Someone just showed me that in a JavaScript object literal, you can use a variable instead of a key-value pair. This results in a property with the variable's name as the key and the variable's value as the value:

var someProperty = "someValue";
var someObject = {
  someProperty
};

//Variable name became property name
alert(someObject.someProperty); //Outputs "someValue"

I'd never heard of this syntax before, but we ran it in the Chrome console and it worked. I tested this in the following browsers, and here are the results:

  • Chrome 51: works
  • Firefox 38: works
  • Internet Explorer 11: errors with Expected ':'
  • Edge 25: works

My questions are

  1. Is there a name for this feature?
  2. Is this standardised? If so, which standard? ES6?
  3. Is there documentation for this somewhere? (Such as on MDN)
Sam
  • 40,644
  • 36
  • 176
  • 219

1 Answers1

2

It is part of ES6, check for shorthand properties http://es6-features.org/#PropertyShorthand

Srle
  • 10,366
  • 8
  • 34
  • 63