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

126
Views
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 answers
Answer question

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 Report

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 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!