Home > Software engineering >  Getting Undefined error at the end of sets Iteration in JavaScript
Getting Undefined error at the end of sets Iteration in JavaScript

Time:02-08

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);

CodePudding user response:

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.

CodePudding user response:

what you probably need

   let itrate = []
    fruits.forEach(fruit =>{
      itrate.push(fruit)
     });
        
    console.log(itrate);
  •  Tags:  
  • Related