views:

38

answers:

1

Hello. 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  A: 

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.

Eli Bendersky
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.
Dennis