DEV Community

loading...

Object Shorthand

codefinity profile image Manav Misra ・1 min read

'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 = ["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 = ["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.

Discussion (0)

pic
Editor guide