0

Why do people say that javascript doesn't have 2-dimensional arrays? Isn't

[[]]

A 2-dimensional array?

Evanusso
  • 129
  • 1
  • 10
  • 2
    Who are these people? – slider Nov 28 '18 at 01:30
  • 4th answer of [this question](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) for example. There are other developers saying that on SO as well. – Evanusso Nov 28 '18 at 01:33
  • 2
    that post is 9 yrs ago. should be outdated – ACD Nov 28 '18 at 01:34
  • in my experience a 2 dimensional array is more like [[1,2][2,4]] – Mr-Programs Nov 28 '18 at 01:47
  • maybe they mean it cause arrays are originally meant to have static dimension like in c++ and javascript is very dinamic so they resemble more lists which ure able to modify in sizes and add things in the middle etc – Mr-Programs Nov 28 '18 at 01:48

1 Answers1

1

Well, [[]] is an array containing an array. Also, [[1],[2,3]] is an array containing arrays.

Sometimes an array of arrays can be, and is intended to be, interpreted as a 2D array. Other times it's something different.

While js clearly allows an array to contain arrays, it doesn't provide any special support for 2D arrays as a complete data structure. What that means to you as a developer probably depends on what you expect out of a 2D array.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Is their any other language that provides a special 2D array data structure? – slider Nov 28 '18 at 01:55
  • It's probably more accurate to say that `[[1],[2,3]]` is an array containing *references* to arrays. For example, something like `x = [[1],[2,3]]; y = [x[1]]; y[0][1] = 4;` will cause `x[1][1]` to be `4`, because `x[1]` and `y[0]` are references to the same array. – ruakh Nov 28 '18 at 02:04
  • @slider - In C, there are arrays of arrays and also there are multi-dimensional arrays. The syntax is confusingly similar and often interchangeable (so much so that some tutorials, thinking they're helpful, may suggest that they're the same), but it is possible to define a proper 2D array held in a single block of memory such that the compiler "knows" how to increment or index past a row at a time. Most sufficiently advanced versions of BASIC have multidimensional arrays with distinct syntax. – Mark Adelsberger Nov 28 '18 at 02:26
  • @MarkAdelsberger: I think you're a bit confused. In C, a multidimensional array *is* an array of arrays. (And yes, it's held in a single block of memory.) I guess you're under the impression that "array of arrays" means "array of pointers to arrays" (or "array of pointers to first elements of arrays"), but no, those are separate things. – ruakh Nov 28 '18 at 18:20
  • @ruakh - No, I am not confused. Sorry you disagree with my phrasing; it's abbreviated to deal with the comment format, which is why follow-up questions don't belong in comments to begin with. But you're wrong in saying that a C multi-dim array is merely an array of arrays, for reasons I did express. – Mark Adelsberger Nov 28 '18 at 21:12