I have the array
var data = [name, address, city, country];
And the loop
var columns;
for (var i = 0; i < data.length; i++) {
columns += "data[" + i + "], ";
}
columns = columns.slice(0, -2);
alert(columns);
The alert message says
undefineddata[0], data[1], data[2], data[3]
What am I doing wrong here? I want to remove the undefined...
I have the array
var data = [name, address, city, country];
And the loop
var columns;
for (var i = 0; i < data.length; i++) {
columns += "data[" + i + "], ";
}
columns = columns.slice(0, -2);
alert(columns);
The alert message says
undefineddata[0], data[1], data[2], data[3]
What am I doing wrong here? I want to remove the undefined...
Share Improve this question edited Oct 22, 2010 at 0:10 John Kugelman 362k69 gold badges548 silver badges595 bronze badges asked Oct 22, 2010 at 0:07 MorganMorgan 2772 gold badges7 silver badges13 bronze badges1 Answer
Reset to default 17You need to start with an empty string, like this:
var columns = "";
Right now what you have is basically equivalent to:
var columns = undefined;
Which when concatenated to a string, gives you "undefined"
.