0

const person = (firstName, lastName) => 
{
  first: firstName,
  last: lastName
}
console.log(person("Jill", "Wilson"))

This code get a syntax error Please let me know the correct code Thanks

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • And the exact error is...? – j08691 Jun 23 '21 at 16:06
  • @j08691 [`last:`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) is not valid as part of a comma operator expression. – VLAZ Jun 23 '21 at 16:11

1 Answers1

2

Wrap your object in brackets (). It is required if you are returning anonymous object right in arrow function.

const person = (firstName, lastName) =>
({
  first: firstName,
  last: lastName
})
console.log(person("Jill", "Wilson"))

Why?

It could not be possible to find difference between function and object. This is function in curly brackets:

const myFn = () => {
   const someCode = "wow I'm in function. Not in Object!";
}

It has same syntax, same brackets, as the example you have provided.

Jax-p
  • 7,225
  • 4
  • 28
  • 58