Jonathan - 1 year ago 54

Javascript Question

What is the location of x and y in this array:

`var a = [ [1,2], [x], [3, [y]] ];`

I'm a bit confused about the syntax here.

Would it be:

`a[1] = x and a[2][1] = y?`

Or does the introduction of a new set of square brackets mean we instead have:

`a[1][0] = x and a[2][1][0] = y?`

Answer Source

let's find out together ;)

```
var a = [ [1,2], [x], [3,[y]]];
```

if you write that line by line:

```
var a = [
[1,2], // new array with index 0 containing 1 and 2 => a[0][0] = 1 and a[0][1] = 2
[x], // new array with index 1 containing x => a[1][0] = x
[3,[y]] // new array with index 2 containing 3 and array containing y a[2][0] = 3 a[2][1][0] = y
];
```

in other words, x = a[1][0]