Javascript: Working with Two dimensional arrays

You can work with one dimensional arrays by specifying the array elements in square brackets.

var colors = ["blue", "red", "white", "green"];

Now if you want to work with two dimensional arrays, you can make a one dimensional array as one of the elements specified in square brackets

var codes = [ [1, 2, 3, 4], [5, 6, 7, 8]]

Now to access each element, you have to specify both the row number and column number


You can also have varying length for a row

var codes = [ [1, 2], [5, 6, 7, 8]]

As you can see row 1 has length 2 and row 2 has length 4.
If you access an array element which is not present, you will get undefined


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s