最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 17

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)

  1. 暂无评论