problem "undefined"
rottenpooh (0)

Hi, I want to show all "contact" of a list and it works but after each contacts the console write "undefined". I code in JavaScript.
Is there someone who knows what's the problem ?
there is my code (the name of the variable are in French):
class Contact {
constructor(nom,prenom) {
this.nom = nom;
this.prenom = prenom;
}
decrire() {
return console.log(nom : ${this.nom}, prénom : ${this.prenom});
}
}

const contacts = [];
contacts.push(new Contact("Lévisse","Carole"));
contacts.push(new Contact("Nelsonne","Mélodie"));

function lister() {
console.log("Voici la liste de tous vos contacts :")
return contacts.forEach(contact => {
console.log(contact.decrire()); //problème "undefined"
});
}
lister();

You are viewing a single comment. View All
malvoliothegood (107)

You do not need to return theconsole.log. You are getting the undefined because you are doing so. Returning a function call of a function that is not designed to return anything, as console.log is, will result in undefined being returned. I hope this makes sense.

rottenpooh (0)

Thanks for your help, I try what you said but it does not work, apparently the problem is at the line 40 which is "console.log(contact.decrire());". But you have right that the "return" is useless.