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

200
Vistas
I can't understand why it returns undefined

I am trying to add 2 to given array. But the code returns undefined when run. I am trying to learn ES6, I couldn't find the problem. I need help.

const mapSomething = (arr) => {
  arr.map(n => {
    return n + 2;
    
  })
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It is logging undefined because the function is not returning any value.

You have to return the result from Array.map:

const mapSomething = (arr) => {
  return arr.map(n => {
    return n + 2;
  })
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

Also take note that the return statement in your arrow function is redundant, since there is only one expression being evaluated:

const mapSomething = (arr) => {
  return arr.map(n => n + 2)
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't have a return statement in the function, so it returns undefined by default.

If an arrow function is a single expression that you want to return, you shouldn't put {} around it. That makes it a normal function body, which requires an explicit return statement (like you have with n + 2).

const mapSomething = (arr) => arr.map(n => n + 2);

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Denunciar

0

const mapSomething = (arr) => {
    const arrData = arr.map(n => {
      return n + 2;     
    })

    return arrData
  }
  
  // It have to return [3, 4, 5] but it returns undefined
  console.log(mapSomething([1, 2, 3]))
  
  //NOTE: you where not returning anything from the function itself

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