DEV Community

Discussion on: How do you write an empty array?

mbougarne profile image
Mourad Bougarne

Thank you Ryan!!! You teach me something new. i think here in this example: [[],[],[]] it's a 3d array?!

Thread Thread
shiftyp profile image
Ryan Lynch (he/him) Author • Edited on

You can tell how many dimensions an array has by how many individual indices you need to access the values. let's add some values to the above array and access one:

const arr = [[1],[2],[3]]

const value = arr[0][0] // value is 1

Notice that we need two indices to access the values. An array of three dimensions would be nested such that you need three indices. For example:

const arr = [[[1],[2]],[[3],[4]],[[5],[6]]]

const value = arr[0][0][0] // value is 1
Thread Thread
mbougarne profile image
Mourad Bougarne

Thanks so much Ryan, it makes a lot of sense now.