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

155
Views
Getting Undefined error at the end of sets Iteration in JavaScript

I am getting an undefined error after logging each element of the set. please have a look at the code snippet bellow

  let arrayFruits = ['apple', 'banana', 'orange', 'plum', 'peach', 'strawberry', 'raspberry'];

const fruits = new Set(arrayFruits);

const itrate = fruits.forEach(fruit =>{
    console.log(fruit);

});

console.log(itrate);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

forEach do not have any return value. Undefined is printed as part of iteration even if you remove console.log(fruit) from iteration you will still see undefined printed in console.

about 4 years ago · Juan Pablo Isaza Report

0

Please do mention what your use case is when asking the question.

According to the documentation, forEach returns undefined.

console.log is also undefined as it does not explicitly return anything.

You can essentially write your function like this to understand it better.

let arrayFruits = ['apple', 'banana', 'orange', 'plum', 'peach', 'strawberry', 'raspberry'];

const fruits = new Set(arrayFruits);

fruits.forEach(fruit =>{
    console.log(fruit);
});

This will print all the values in your set. If you copy-paste this snippet into a browser's console such as Chrome, it will log the result of the function as well. In this case, forEach returns undefined so undefined will be printed at the end.

about 4 years ago · Juan Pablo Isaza Report

0

what you probably need

let arrayFruits = ['apple', 'banana', 'orange', 'plum', 'peach', 'strawberry', 'raspberry'];
    
const fruits = new Set(arrayFruits);
    
let itrate = []
fruits.forEach(fruit =>{
  itrate.push(fruit)
});
            
console.log(itrate);
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!