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

176
Views
Javascript syntax problem (find the max value)

The code is find the max value

  1. How this code work
// this is original code
const items = [
    { id: 0 },
    { id: 1 },
    { id: 2 }
]

function id(items) {
    const maxId = items.reduce((maxId, items) => 
        Math.max(items.id, maxId), -1
    )
    return maxId + 1
}
console.log(id(items))  // I get 3

The reduce methods callback (maxId, items) => Math.max(items.id, maxId), -1

I can not figure out what is this, it just return something, -1

I never seem this syntax ever.

A comma between two value, what I expect is return [ , ] or ( , )


  1. Why this code get different result

I also tried this below, I think its same code above. But result is not same.

// I changed something, I add { return  something in reduce callback }
const items = [
    { id: 0 },
    { id: 1 },
    { id: 2 }
]

function id(items) {
    const maxId = items.reduce((maxId, items) => {   // Here
            return Math.max(items.id, maxId), -1     // add
        }                                            // { return }
    )
    return maxId + 1
}
console.log(id(items))  // I get 0

about 4 years ago · Juan Pablo Isaza
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!