0

Again I'm still new to this javascript thing, so just would like to know if there is another way of setting the values of an array (just like declaring it);

//correct way of declaring an array and reusing
var adata = new Array('1','2','3');
//reusing of variable
adata[0] = '4';
adata[1] = '5';
adata[2] = '6'

This part is my question; I want to declare the values of the array just like declaring them to minimize the number of lines;

//array declaration
var data = new Array('1','2','3');
//reusing of variable
data = ['4','5','6']; ---> (as an example) I get an error msg "Invalid assignment left-hand side"

is this possible? If so, what is the correct syntax? I hope I'm making sense.

Thanking you in advance.

1 Answer 1

1

Although I wouldn't call it "reuse", this works for me:

>>> var data = new Array('1', '2', '3');
>>> data = ['4', '5', '6']

The >>>'s come from the terminal (I'm using the Firebug console).

This makes the variable data reference another array. Recall that variables in JS are actually references that kinda "point to" objects.

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

1 Comment

So stupid of me, the declaration actually works. I tried to thoroughly check my javascript. There was this + tag that I forgot to remove. Thanks for your reply though.

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.