Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda