开发者

FOR loop and string concatenating with JavaScript gives me an undefined value

开发者 https://www.devze.com 2023-01-20 17:06 出处:网络
I have the array var data = [name, address, city, country]; And the loop var columns; for (var i = 0; i < data.length; 开发者_运维技巧i++) {

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...


You 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".

0

精彩评论

暂无评论...
验证码 换一张
取 消