DEV Community

Manav Misra
Manav Misra

Posted on • Edited on

Object Shorthand

'Collecting' an Object Pre-ES2015/ES6

Give Given some existing 'in scope' variables, if we wanted to 'collect' them into an object, before ES2015, we had to do:

const fname= "Mark";
const lname = "Galloway";
const aliases = ["Mean Mark", "The Undertaker", "American Bad Ass"];
const status = "retired";

const wrestler = {
  fname: fname,
  lname: lname,
  aliases: aliases,
  status: status
}
Enter fullscreen mode Exit fullscreen mode

'Collecting' An Object ES6+

Object Shorthand

const fname= "Mark";
const lname = "Galloway";
const aliases = ["Mean Mark", "The Undertaker", "American Bad Ass"];
const status = "retired";

/**
  * If there is no ':',
  * the OBJECT LITERAL automatically 'creates' a STRING 🔑
  * and assigns the VALUE BOUND TO the VARIABLE IN SCOPE
  * that has the same name. 🦄
  */
const wrestler = {
  fname,
  lname,
  aliases,
  status
}
Enter fullscreen mode Exit fullscreen mode

Summarily, this is a nice way to save some typing. Less typing ➡️ fewer mistakes and 🐛s.

Top comments (0)