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

129
Views
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 answers
Answer question

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 Report

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 Report
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!