I’m iterating over an object and I want to concatenate the name of the service. This is my code:
var servizi; for(var i = 0; i < appointment.id_services.length; i++) { servizi += appointment.id_services[i].name + " "; }
Now the problem’s that I got this result:
undefined hair cut
In my object there’s only hair and cut, why I get undefined also?
Answer
You get undefined because you declared an uninitialized variable, and then added to it (twice).
Initialize the declared variable as an empty string first
var servizi = "";