Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question

marked as duplicate by Yotam Omer, Jimbo, gustavohenke, smerny, Steven V Jul 21 '13 at 20:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
This is one of the reasons why not to use new Array(). If you pass only one argument, you indeed set the length of the array to that argument. But if you pass more than one, it generates an array containing those elements. There is no need to use Array(), use literal notation instead []. – Felix Kling Feb 10 '11 at 20:28

3 Answers 3

up vote 4 down vote accepted

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]
         ];
share|improve this answer
2  
so what is the answer ? – Shlomi Komemi Feb 10 '11 at 20:14
    
I believe OP was trying to create a multi-dimensional Array with that syntax. EDIT: This comment is no longer relevant. – user113716 Feb 10 '11 at 20:16
    
You are right. I was told it creates a multi-dim array. Thanks for good and clear answer – Dan Feb 10 '11 at 20:20

The code you posted makes an array consisting of two integers. You're then trying to treat an integer as an array.

mv = new Array();
mv[0] = new Array();
mv[0][0] = "value1-1";
mv[0][1] = "value1-2";

mv[1] = new Array();
mv[1][0] = "value2-1";
mv[1][1] = "value2-2";

There is no way to directly instantiate a multidimensional array.

share|improve this answer
    
In the second block, don't you mean mv[1][0] and mv[1][1]? – JLewkovich Oct 15 '14 at 17:22
    
@JL Yep, corrected – Chris Baker Oct 15 '14 at 18:42
    
The only script that solved it for me with 3 dimentions... Thx. – JanBorup Aug 26 at 13:55

you want to create an array of arrays, but you're creating an array with 2 elements:

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

if you want to create a multidimensional array, you have to think in terms of array of arrays.
this way, a 2 dimensional array (or matrix) would be defined as :

var a = [[],[]];//or var a = new Array([],[]);
//or if you want to initialize the matrix : 
var b = [
    [1,2],
    [3,4]
];
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.