-1
var test = [[
  [1,2],[2,3],[3,4]
]];
alert(test[[0]].length);

This returns me 3, but I cannot understand what this actually mean. How come this result?

5 Answers 5

1

There are no multi-dimensional arrays in JavaScript. There are only nested arrays.

[            //  test
  [          //    test[0]
    [1,2],   //      test[0][0]
    [2,3],   //      test[0][1]
    [3,4]    //      test[0][2]
  ]          //
]            //

As you can see, test[0] has a length of three.

And test[[0]] is semantically incorrect(*) and collapses into test[0].


(*) The index operator ([]) expects a number, like in test[0]. If you don't pass a number to it (like in your test[[0]] case, where you pass the array [0]), a conversion to string will happen first. (This is because of the first note below.)

Arrays are converted to string by joining their members with a comma. [0].toString() is "0", and therefore test[[0]] is equivalent to test["0"], which is equivalent to test[0].

Notes:

  • The square brackets are used for property access as well, so that test["length"] is the same as test.length.
  • Consequently, something horrible like test[[0]][["length"]]) is equivalent to test[0].length and will give you 3.
  • Something like test[[0,0]] would be test["0,0"] - and since there is no property named "0,0" on that array, you will get undefined.
Sign up to request clarification or add additional context in comments.

Comments

0

The array test[0] contains three items:

  • [1,2]
  • [2,3]
  • [3,4]

Hence, the result of the length is 3.

The length of array test is just one, since test contains only one array:

  • [1,2],[2,3],[3,4]

In fact this is not a multi-dimensional array, it's just an array containing arrays (called a jagged array, or nested array).

Comments

0

test[[0]] is same thing as test[0] and test[0] is an array

[1,2],[2,3],[3,4]

consisting of these elements.

if you want to access ,for instance, [2,3] you need to use this syntax:

test[0][1]

and test[0][1].length will give you 2.

2 Comments

if these syntaxes are same i.e. test[[0]] and test[0] then why is this supported?
@PatrickHofman No, it converts [0] to string, which is why test[[0]] works and test[[0,0]] doesn't.
0

This is a nested array. An array of arrays.

It will be more clear if you expand your example:

var test = [
    [
        [1,2],[2,3],[3,4]
    ], 
    [
        [5,6],[7,8],[9,9],[6,7]
    ], 
];
alert(test[[1]].length);

Now, your test is an array of two arrays. The first one of which is an array of 3 arrays. The second one is an array of 4 arrays.

Now, test.length is 2. test[0].length is 3. test[1].length is 4. [[n]] just collapses.

Demo: http://jsfiddle.net/abhitalks/6H7Lj/

Comments

0

If you want get the length of first arrays length then write

alert(test[0][0].length);

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.