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

141
Visualizações
What is the role of parentheses in this javascript expression?

I came across this short snippet (HERE)

const pick = (obj, arr) =>
  arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});

And I don't understand the role of parentheses in this part

(curr in obj && (acc[curr] = obj[curr]), acc)
() => (... && (... = ...), ...)

// First, parentheses after the arrow function means we are returning what is inside
() => (...)

// This looks like a short if statement
curr in obj && ...

// Why are we grouping the assignment into parentheses here?
(acc[curr] = obj[curr])

// What does (..., ...) is supposed to to 
() => (... && (...), acc)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The parentheses are required because otherwise, this expression:

curr in obj && acc[curr] = obj[curr]

would be invalid syntax, because the left-hand side of the = would evaluate to curr in obj && acc[curr] (an expression), not a reference that can be assigned to.

But this is horrible code - it's quite confusing. A much better version would be

const pick = (obj, arr) => {
    const output = {};
    for (const curr of arr) {
        if (curr in obj) {
            output[curr] = obj[curr]
        }
    }
    return output;
};

That's so much easier to understand at a glance, isn't it?

Another option is to filter the entries of the object:

const pick = (obj, arr) => Object.fromEntries(
  Object.entries(obj)
    .filter(([key]) => arr.includes(key))
);
about 4 years ago · Juan Pablo Isaza Relatório

0

This:

(curr in obj && (acc[curr] = obj[curr]), acc)

is another way of writing the following:

if (curr in obj) {
   acc[curr] = obj[curr];
}

return acc;

Not only the one-liner is less readable, it can also be considered as abusing the comma operator.

Code readability is more important than writing less lines of code.

// What does (..., ...) is supposed to do

() => (... && (...), acc)

Parenthesis in the following expression:

(curr in obj && (acc[curr] = obj[curr]), acc)

ensure that:

  1. Overall expression is evaluated as a single expression that consists of multiple sub-expressions

  2. Assignment is evaluated without the curr in obj && part because without those parenthesis, curr in obj && acc[curr] = obj[curr] is invalid syntax.

    With parenthesis, the expression:

    (acc[curr] = obj[curr])
    

    will evaluate to the value of obj[curr]. So the expression:

    curr in obj && (acc[curr] = obj[curr])
    

    will become:

    curr in obj && <assignment 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