Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
JS return structure

What is the difference between {} and ( ) in JavaScript's return statement?

const fruits = [
  {
    name: "FaceBook",
    nickname: "FB",
  },
  {
    name: "Youtube",
    nickname: "YT",
  },
  {
    name: "AmazonWebService",
    nickname: "AWS",
  },
];
const count = fruits.reduce((acc, cur) => {
  return { ...acc, [cur.nickname]: cur.name };  // What does {} of return mean here?
}, {});
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Since return is not a function, but a statement, It is syntactically similar to other simple control flow statements like break and continue that don't use parentheses either.

So parentheses in return are just like parentheses anywhere else, its for order of calculations or just grouping.

But when you use curly brackets in return its for returning a new object, that's also applicable anywhere else.

In conclusion they have nothing to do with the return statement.

about 4 years ago · Juan Pablo Isaza Relatório

0

{} is a literal object initializer used to construct a new initialized object.

() is simple grouping of an expression. return ( ...acc, [cur.nickname]: cur.name ) would return the last expression cur.name as the comma operator evaluates left to right and return the last expression.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are using {}, it will return it as an object, so you need to add a key into it.

See this example below for more explanations:

function test1() {
  return 'ok'
}

function test2() {
  return ('ok')
}

function test3() {
  return { message: 'ok' } // it won't accept: return {'ok'}
}

console.log(test1())
console.log(test2())
console.log(test3())

In your case, the [cur.nickname] is the key. And the cur.name is the value

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda