Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 864 Bytes

object-initialization-with-shorthand-property-names.md

File metadata and controls

39 lines (32 loc) · 864 Bytes

Object Initialization With Shorthand Property Names

If I have some variables:

const one = 1,
  two = 2,
  three = 3;

and I'd like to initialize an object with them, I'll generally do something like the following:

const obj1 = {
  one: one,
  two: two,
  three: three
};
// Object { one: 1, two: 2, three: 3 }

That seems pretty standard, but with ES6 comes a feature called shorthand property names which makes that look verbose and redundant. If you already have properly named variables, they can be used as a short hand for both the key name and variable value:

const obj2 = {
  one,
  two,
  three
};
// Object { one: 1, two: 2, three: 3 }

See the MDN Docs for Object Initializer for more details.