Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda