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

179
Visualizações
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
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