Go Back

JavaScript: List of Variables vs. Object

Posted on 2018-12-09

Recently, I received a question on some JavaScript stuff:

im not sure how it helps passing an object instead of a list of variables…

Code with a more precise explanation

Before:

function createMessageWithListOfVars(firstName, lastName, age) {
  return `${firstName} ${lastName} is ${age} years old.`
}

console.log(createMessageWithListOfVars('Andreas', 'Max', 30))

After:

function createMessageWithObjectDestruct({ firstName, lastName, age }) {
  return `${firstName} ${lastName} is ${age} years old.`
}

console.log(
  createMessageWithObjectDestruct({
    firstName: 'Andreas',
    age: 30,
    lastName: 'Max',
  })
)