Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  50] [ 7]  / answers: 1 / hits: 25349  / 14 Years ago, thu, february 10, 2011, 12:00:00

Today I've heard that it's possible to create a multi - dimensional array in js using this syntax:



var a = new Array(3,3);
a[2][2] = 2;
alert(a[2][2])


However this doesn't work in opera. Am I wrong somewhere?


More From » javascript

 Answers
31

Yes, you are wrong somewhere. var a = new Array(3,3); means the same as var a = [3,3];. It creates an array with two members: the Number 3 and the Number 3 again.



The array constructor is one of the worst parts of the JavaScript language design. Given a single value, it determines the length of the array. Given multiple values, it uses them to initialise the array.



Always use the var a = []; syntax. It is consistent (as well as being shorter and easier to read).



There is no short-cut syntax for creating an array of arrays. You have to construct each one separately.



var a = [ 
[1,2,3],
[4,5,6],
[7,8,9]
];

[#93793] Wednesday, February 9, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
;