1

I have programmed in Microsoft Small Basic in the past, which can have arrays like this:

Array[1][1] = "Hello"
Array[1][2] = "Hi"
Array[1][2] = "Hey"

Now, in Javascript, I know how to create a single array (var Array = New Array()) but are there any array types like the ones above?

5
  • 3
    Array elements may be numbers, strings, objects ... or arrays. Just stack 'em up. BTW prefer var array = []; to var array = new Array(); Commented Jan 9, 2013 at 22:24
  • What does "I have some small basic" mean? Commented Jan 9, 2013 at 22:25
  • @LightnessRacesinOrbit Presumably "I have used a little BASIC in the past." Commented Jan 9, 2013 at 22:26
  • @cdhowie: Sure, were that valid BASIC syntax. Commented Jan 9, 2013 at 22:26
  • That is "Small Basic". A programming language Commented Jan 9, 2013 at 22:33

5 Answers 5

8

There are no true multidimensional arrays in JavaScript. But you can create an array of arrays like you have done.

JavaScript's arrays are just objects with a special length property and a different prototype chain.

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

3 Comments

Thanks! Thats all I needed to know.
"Small Basic" is a programming language.
Sorry. It needed some clarification :)
2

Yes, you need to create an array of arrays:

var x = new Array(3);
x[0] = new Array(3);
x[1] = new Array(3);
x[2] = new Array(3);

x[0][0] = "Hello";
etc.

Remember that indexing is zero-based.

Edit

Or:

var x=[];
x[0] = [];
x[1] = [];
x[2] = [];
...
x[0][0] = "Hello";

etc.

3 Comments

@LightnessRacesinOrbit I think it's fine if used for the behaviour of setting the length. If they were empty, [] would definitely be better.
@alex: It's pretty rare to need to set an array length beforehand.
@LightnessRacesinOrbit I do it often, generally in conjunction with join().
0

You can achieve this:

var o = [[1,2,3],[4,5,6]];

Also you can use the fact that objects in javascript are dictionaries:

var o;
o["0"] = {'0':1, '1':2, '1':3};
var x = o["0"]["1"]; //returns 2

Comments

0

The easiest way would be to just declare an array, and initialize it with a bunch of other arrays. For example:

var mArray = [
  [1,2,3],
  [4,5,6]
  ];

window.alert(mArray[1][1]); //Displays 5

As others have pointed out, this is not actually a multi-dimentional array in the standard sense. It's just an array that happens to contain other arrays. You could just as easily have an array that had 3 other arrays, an int, a string, a function, and an object. JavaScript is cool like that.

Comments

0

You can create arrays statically in JS like this:

var arr = [
   [1, 2, 3, 4], 
   [8, 6, 7, 8]
];

Note that since this is not a true "multidimentional array", just an "array of arrays" the "inner arrays" do not have to be the same length, or even the same type. Like so:

var arr = [
   [1, 2, 3, 4], 
   ["a", "b"]
];

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.