6

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.