Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Why does returning one arrow function work and the other not?

Both arrow functions should return the result, but why does only the first one work?

It works:

const createUser = (name, email) => ({name, email})
const user1 = createUser("User Test", "test@test.com")
console.log(user1)

Does not work:

const createUser = (name, email) => {name, email}
const user1 = createUser("User Test", "test@test.com")
console.log(user1)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is because in

const createUser = (name, email) => ({name, email})
const user1 = createUser("User Test", "test@test.com")
console.log(user1)

function is returning an object. This snippet is similar to

const createUser = (name, email) => { return {name, email}}
const user1 = createUser("User Test", "test@test.com")
console.log(user1)

Whereas in the second snippet, the function is not returning anything. It is a simple arrow function with no return value which is why console.log is undefined.

const createUser = (name, email) => {name, email}
const user1 = createUser("User Test", "test@test.com")
console.log(user1)

about 4 years ago · Juan Pablo Isaza Report

0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#advanced_syntax

In an arrow function, to return an object literal expression requires parentheses around expression.

Therefore, your first arrow function has the correct syntax for returning an object literal, and the second one does not.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!