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

121
Vistas
Javascript: return statement inside .map array function gives undefined

Returns undefined. How ever if i console.log inside the map function that prints right value.

const myfun = (data) => {
    data.map((elem) => {
      return elem[1]  
    });
  };
  
  
let demo = [[1,2,3],[1,2,3]]
  
console.log(myfun(demo)); 
// gives undefined
// should give 2 2
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Yes your function returns nothing, therefore undefined.

I think what you meant to write was this.

const myfun = (data) => (
    data.map((elem) => {
      return elem[1]  
    })
);

notice how the myfun function is not using curly braces instead it's using regular brackets.

or this.

const myfun = (data) => data.map((elem) => {
      return elem[1]  
    })

If you use curly brackets on an anonymous function you must explicitly use the return keyword.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This happens because you need to return the map, not just what is mapped

const myfun = (data) => {
    // You need to return the map
    return data.map((elem) => {
      return elem[1]  
    });
  };
  
  
let demo = [[1,2,3],[1,2,3]]
  
console.log(myfun(demo)); 

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